.NET Forum / .NET Framework / .NET SDK / August 2006
force firing of timer
|
|
Thread rating:  |
David Thielen - 23 Aug 2006 23:18 GMT Hi;
First, I need a worker deamon in my ASP.NET web app. Is System.Timers.Timer the best way to do this?
Second, I set the timer to fire once and each time it fires, I do the needed work, determine how long until needed again, and then set that interval and set Enabled = true.
But, sometimes I get new data which will change when I need to fire the timer next. Is there a call that will cause the timer to fire as though it had expired? And what happens if I call that while the timer expired and it is already in it's event handler?
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
"Peter Huang" [MSFT] - 24 Aug 2006 11:58 GMT Hi Dave,
Can you elaborate detailed about what is the Daemon will actually do? Is it a Web Application level or while website level daemo?
As we know IIS 6 will run Web App in a separate App pool(another process) which may be recycled by IIS due to many possibilities. Also if you start the Timer in the web app's page load event, it may be called multiple times because of the client press F5 to refresh the webpage.
Based on my experience another process e.g. a windows service is the property app type for a daemon application which will keep running and will not be affected by other factors.(e.g. IIS running status).
If you have any concern, please feel free to let me know.
Best regards,
Peter Huang
Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 24 Aug 2006 14:20 GMT Hi;
I'm starting it in the web apps application_start method. What it does is at set times it generates reports and then saves that report to a database and/or emails it out.
If a user creates another scheduled report and that new report will run before the previous next scheduled report, then I need to fire the timer event handler to reset to the time of the latest nest report.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> Hi Dave, > [quoted text clipped - 22 lines] > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. "Peter Huang" [MSFT] - 25 Aug 2006 07:29 GMT Hi Dave,
From your description, I understand that you want a daemon application which will do some regular DB job and send report/Email in the defined time period.
Now if we create/start the Timer in Application_Start, it will not affected by Page_Load, but if the IIS recycle the app pool, the application will restart, that is to say the time schedule will mess up. Also for an easy implement was to use an separate thread(create in Application_Start), and use a while loop, it will keep check certian var to do the job and sleep for a regular time.
But due the IIS still may affect this, I suggest you create a Windows Service as another process and communicate with the Web Application to do the job. Based on my knowledge, some large project use the method. The Web Application will send the job as a file to a defined folder, and the windows service will keep monitor the folder, once a new file is added, it will take it/process it and delete it.
If you have any concern, please feel free to let me know.
Best regards,
Peter Huang
Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 25 Aug 2006 16:56 GMT Hi;
The timer is working fine for this. If IIS recycles it will look at the next pending report and run it if due, otherwise sleep until it is due. So recycling is not a problem
I assume the time is called in a seprate thread so it should be similiar to having a worker thread.
I don't want to do a service because I then have to make sure both get installed and started and that is one more thing to go wrong.
The one thing I need is when I add a report to the event queue, it may be due to run before the event that was previously at the top of the queue. So I need a way to force the timer to fire so the event handler can determine what the next report is now.
How can I cause a timer to fire? And only if it is waiting to fire, not if it just fired and the event handler is presently running.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> Hi Dave, > [quoted text clipped - 29 lines] > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. Greg Young - 26 Aug 2006 02:33 GMT The way most people handle this is to sleep for some predetermined amount of time .. wake up see if there is work to do, if not go back to sleep for some predetermined amount of time. This could be 1 second, 1 minute, 1 hour, 1 day etc ... The expense associated with waking up and checking to see if there is work to do should be so minimal as to not have a performance implication. Generally this is also done with a single thread ... if there are other threads they simply fire when they are given a job. Essentially you end up with a single scheduler feeding many workers.
Again there is a slight amount of overhead associated with this but the alternative is a bit more difficult (keep current jobs and have a blocking queue) .. read from the blocking queue with a timeout of x ... if you hit the timeout it is time to run a job you currently have .. if someone puts a new job in your queue you get woken up to take it out of the queue, put it in your jobs section and go back to blocking on the queue with a new timeout.
I don't think your jobs in memory should be kept in a queue (only sent to scheduling through a queue). I say this because when you are storing them in memory you would have to insert into the middle of the queue at times (which kind of makes it no longer a queue more like a sortedlist :))
Cheers,
Greg
> Hi; > [quoted text clipped - 58 lines] >> This posting is provided "AS IS" with no warranties, and confers no >> rights. David Thielen - 26 Aug 2006 15:00 GMT Hi;
It can be done that way. And I store my queue in the database, not in memory so the list order is just a datetime column in the database.
But... If there was a way to cause the timer to fire, that would be a cleaner solution than having it fire every 15 minutes in case a new event was just added. Especially because to check what is next I have to do a query on the database.
However, I am assuming based on no one listing a way to do it with all my questions that there is no way to cause the timer event to fire other than setting it's interval.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> The way most people handle this is to sleep for some predetermined amount of > time .. wake up see if there is work to do, if not go back to sleep for some [quoted text clipped - 84 lines] > >> This posting is provided "AS IS" with no warranties, and confers no > >> rights. "Peter Huang" [MSFT] - 28 Aug 2006 04:49 GMT Hi Dave,
Where did you want to force the timer to fire? From your description, I understand that you will have a timer start, and add event handler function(e.g. OnTimer), When the Timer is fired, the OnTimer func will be called. So if you want the timer to fire before the elapsed time is due, why not call the OnTimer method directly?
Based on my research, the Timer class did not have such a method that will fire an event before the elapsed time is due other than setting it's interval.
Also I do agree with Greg's suggestion, that is what we commonly do. Thread A will keep generated job and put it into Queue, Here I understand the Queue is in the DB per your description. Thread B will schedule to check the Queue every e.g. 15 minutes, once it found a new job, it will schedule another thread(e.g. C) to process the job. So the Thread B will not be blocked for a long time, because it just dispatch the jobs, but not process the job.
If you have any other concern, please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
David Thielen - 28 Aug 2006 14:26 GMT My concern with calling the event handler directly is what if the timer fires while I am in my event handler. It is not designed to be re-entrant. It was an easy fix if I could force the timer to fire. I think because I can't I will have to do the worker thread.
 Signature thanks - dave david_at_windward_dot_net http://www.windwardreports.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> Hi Dave, > [quoted text clipped - 29 lines] > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. Greg Young - 28 Aug 2006 20:57 GMT lock(someLockObject) { //code }
You may run into the case where you call it while the timer is already running or you may call it then immediately after the timer will fire but it will prevent it from being run concurrently.
I would try to stay away from the delta based polling though.
Cheers,
Greg
> My concern with calling the event handler directly is what if the timer > fires [quoted text clipped - 38 lines] >> This posting is provided "AS IS" with no warranties, and confers no >> rights. "Peter Huang" [MSFT] - 29 Aug 2006 04:15 GMT Hi Dave,
In addition to Greg' suggestion.
I do understand your scenario, and that is why suggest you use the Thread approach as I my last reply. Because if we goes to the Thread approach, we can Use a dedicated Thread(e.g. S) to query the job Queue and dispatch the job which should be in a short time and we have decicated thread to handle each job. So that if Thread S dispatch job A to Thread A, if the Thread A have not finished and now a new job B arrived, and a new Thread B will be created to handle job B, so that the Thread A's job will not be interere. This is the multithreading concurrent approach.
If you have any concern, please feel free to post here.
Best regards,
Peter Huang
Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|