|
If you want to access an object instance from anywhere, the Singleton pattern[^] is the recommended method. Are you sure you want to do this for three forms though? Static/Singletons have their place and uses but can often be the result of poor design, poor undestanding of OOP, lazyness. I'm not saying "don't", just be sure that it is actually required!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Using White in .net
how shall i get the DatagridView object in White
thanks in advance
|
|
|
|
|
Your question is not very clear.
What is 'White'?
Do you mean the color White, or are you referring to a third party library called White?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
In .net we've got several timers, but all of them specify an interval to elapse rather than a specific time to come. I'd like to implement a timer which specifies a specific time, say to be triggered at the time of 10:25:15am (hour:minute:second). Using polling tech to determine if it's the right time to trigger is a very bad way and inefficient, I'd like to put the timer to sleep, then when the time is coming it receives a signal from the system to be woken up and then do its work.
One way to do this is still based on the Threading.Timer, convert how long to wait for the specific time into interval. But is there any mechanism provided by .net directly supporting this scenario to signal the thread at specific time?
Many thanks.
|
|
|
|
|
I'm not aware of any explicit support for a due time.
For a 1-second resolution I would just pick some timer (Windows.Forms/Threading/Timers depending on circumstances) and use DateTime and TimeSpan classes to calculate and set the delay.
For a finer resolution, I'd start with the above, then probably execute some polling loop within the final second, including a Thread.Sleep(ms) with a number of milliseconds fitting the resolution (assuming >= 50)
Whatever you do, keep in mind things could go wrong in several ways:
- your PC might get very busy while calculating and setting the due time, resulting in a late call;
- your PC might get very busy at the due time, so the requested action may actually run somewhat later;
- your PC might be switched off or crash in the mean time.
Depending on your requirements a Windows scheduled task may or may not be a better option.
|
|
|
|
|
Yep, Windows scheduled task may be a way. But it needs to be reused/consumed by other programs, so using Windows scheduled task is out of scope.
|
|
|
|
|
The best way to do what you have requested is to use Threading.Timer as you have already noted.
Timers aren't meant to be used this way though, that is to wake up at a specific time only. Why don't you use the Scheduled task if what you want is to run your service/process at a specific time.
|
|
|
|
|
Yeah, a Scheduled Task might be the way to go.
You could use a Timer, but it might not be very good for that purpose -- have it wait for half (or 90% or something) of the remaining time, check, repeat as necessary.
|
|
|
|
|
|
|
|
Xeno's dichotomy paradox[^] says you get tortoise kebab in the real world...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
quartz.net has a number of timers, a cron type timer being amongst them - may be worth a look
'g'
|
|
|
|
|
thanks, i'll take a look at quartz.net.
|
|
|
|
|
Don't use a timer for this - take a look, rather, at using something like a Monitor where you can specify an elapsed interval before the monitor "wakes up". It would look something like this:
private static readonly object _syncLock = new object();
private bool _cancel;
private void ScheduleProcess(DateTime scheduledDate)
{
if (scheduledDate >= DateTime.Now)
throw new ArgumentOutOfRangeException("The scheduled time is set for a time that has already passed.");
TimeSpan waittime = scheduledDate - DateTime.Now;
lock (_syncLock)
{
Monitor.Wait(_syncLock, waittime);
}
}
public void Cancel()
{
_cancel = true;
lock(_syncLock)
{
Monitor.PulseAll(_syncLock);
}
} Create a background thread, and use ScheduleProcess to block access to your functionality until the time has passed. For instance:
public void DoSomething(DateTime scheduledTime)
{
SmartThreadPool stp = new SmartThreadPool();
stp.QueueWorkItem(new WorkItemCallback(RunProcess), scheduledTime);
}
private object RunProcess(object scheduledTime)
{
ScheduleProcess((DateTime)scheduledTime);
if (!_cancel)
{
}
return null;
} This example uses the SmartThreadPool[^].
|
|
|
|
|
Using Monitor.Wait() is a great solution but comes with its own set of problems. Most notably, it has no guaranteed accuracy. If you tell it to wait one hour, it will wait AT LEAST one hour. A number of factors can delay the response. It would be much safer to use Monitor.Wait() to suspend the thread, then switch to a more accurate but processor-intensive method for precision.
Also, when calculating the delay, since a number of factors (already pointed out by someone else) can cause this delay to be off, if you really want precision, I would calculate it a few times, then perform the reverse calculation for each result. This will allow you to be assured that your delay value is accurate.
|
|
|
|
|
thanks for pointing out this.
|
|
|
|
|
Thanks very much for your example.
|
|
|
|
|
May be you should look at API functions CreateWaitableTimer(), SetWaitableTimer() and CancelWaitableTimer(). SetWaitableTimer() has the ability to specify UTC-based absolute time. Dont't know, if these functions are implemented directly in BCL though. THT.
|
|
|
|
|
|
If you consider one second as precise enough, this should suffice.
When I hear precision timer, I am thinking microsecond or nanosecond accuray.
http://quartznet.sourceforge.net/[^]
|
|
|
|
|
Actually, I also meant 'micorsecond' (well, I'm not sure whether .net can really cater for nanosecond accuray or not)
I'll take look at quartz.net, thanks.
modified on Thursday, September 2, 2010 6:28 PM
|
|
|
|
|
My answer starts in the wayback machine, before things such as .net and controls and whatnot existed. It's an alternate idea, but probably doesn't do what you want.
Anyway, back a long time ago, we had systems which needed to do take certain actions at certain times. We bulit a server application which would go in our data and look for certain conditions. When they were met, an event would happen.
I wonder if you could write a service to do something similar. When you know you want to trigger an event at 10:25:15, you write to an event table, noting the event name and time.
The service is pretty simple; look in the event table for things in the past which are in a state of 'not run'; fire off an appropriate class/method based on event name, mark as 'complete' the event entry. Mark as 'run, but with exception' if the action fails.
I think this solution depends on how time-specific your answer really needs to be. If it's life-critical, this is no good. If it's within a few seconds, this might be a pretty simle solution.
|
|
|
|
|
Definitely, we can do it in this way, but the thing is what techique I can use in .net not to rely on the inefficient polling tech (and polling is not accurate as well). I'd like to let the working thread to sleep and when the specific time coming, woken up. This model is what I really want. I wonder if .net has mechanism directly support this model, don't have to do converting interval stuff.
By the way, if the time-specific matter needs to meet life-critical requirement, what .net mechanism we can rely on? Does .net have such capability to support this level of accuracy? (My understanding is this needs real-time OS and device to support).
|
|
|
|
|
I think you're right on the life-critical aspect. I don't believe there is anything in .net that will give you that granularity.
I don't know if my idea fits your needs, or is even a step down the right path. In the solution I was talking about, we knew every time an action was taken exactly what the next 'automatic' action would be, and exactly when it would occur.
However, for us, not so time-critical. So, even though we were able to come up with a simple 'if this happens, write this event and time to the database' scenario, our server wouldn't necessarily notice that event right at the time written.
But, it did turn out to be a really simple solution to a timer-type problem.
I suppose in today's world, you'd just go ahead and use a Timer and tie it to the event you wanted to perform.
Anyway, good luck.
|
|
|
|
|