Windows service & Timers
-
Hi I am creating a windows service which is using timers. I know I shouldn't use something like DispatcherTimer or Forms.Timer. The problem with other timers(like System.Timers.Timer or System.Threading.Timer) is that its events are raised in background threads. I tried to use the System.Threading.Timer and than use Dispatcher.CurrentDispatcher to marshal the event to the thread that created the timer but this did not work ok. I am asking the following: Can I use Dispatcher.CurrentDispatcher and Dispatcher.Invoke in windows service app. ? If not, can somebody direct me in the right way to solve my problem? Any advice will be appreciated, Uros
-
Hi I am creating a windows service which is using timers. I know I shouldn't use something like DispatcherTimer or Forms.Timer. The problem with other timers(like System.Timers.Timer or System.Threading.Timer) is that its events are raised in background threads. I tried to use the System.Threading.Timer and than use Dispatcher.CurrentDispatcher to marshal the event to the thread that created the timer but this did not work ok. I am asking the following: Can I use Dispatcher.CurrentDispatcher and Dispatcher.Invoke in windows service app. ? If not, can somebody direct me in the right way to solve my problem? Any advice will be appreciated, Uros
I use System.Timers.Timer for my Windows Services and I have no trouble. What problem are you having?
-
Hi I am creating a windows service which is using timers. I know I shouldn't use something like DispatcherTimer or Forms.Timer. The problem with other timers(like System.Timers.Timer or System.Threading.Timer) is that its events are raised in background threads. I tried to use the System.Threading.Timer and than use Dispatcher.CurrentDispatcher to marshal the event to the thread that created the timer but this did not work ok. I am asking the following: Can I use Dispatcher.CurrentDispatcher and Dispatcher.Invoke in windows service app. ? If not, can somebody direct me in the right way to solve my problem? Any advice will be appreciated, Uros
Have a look at the
AsyncOperation
andAsyncOperationManager
classes. The former has aPost
andPostOperationCompleted
methods that will call a method on the required thread via theSendOrPostCallback
delegate, you could raise your event in that method.Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I use System.Timers.Timer for my Windows Services and I have no trouble. What problem are you having?
The problem I am having is that the thread that created the timer and started it, is no longer available when the timer's interval elapses. When I created an instance of the CustomTimer - which is just a wrapper around System.Threading.Timer - I save the Dispatcher.CurrentDispatcher in the private field of the CustomTimer. Than I start the timer end wait for its event to get raised(still inside the CustomTimer) and then use the saved Dispatcher to marshal to the thread that created the timer. But in some casses the Thread property of the stored Dispatcher has the state of stoped or WaitSleepJoin. But I am not sure what is the reason for that - maybe one should not use dispatcher in the windows service? I can put the code here if I was not clear. Thanks for your time, Uros
-
Have a look at the
AsyncOperation
andAsyncOperationManager
classes. The former has aPost
andPostOperationCompleted
methods that will call a method on the required thread via theSendOrPostCallback
delegate, you could raise your event in that method.Dave
Tip: Passing values between objects using events (C#) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi I am creating a windows service which is using timers. I know I shouldn't use something like DispatcherTimer or Forms.Timer. The problem with other timers(like System.Timers.Timer or System.Threading.Timer) is that its events are raised in background threads. I tried to use the System.Threading.Timer and than use Dispatcher.CurrentDispatcher to marshal the event to the thread that created the timer but this did not work ok. I am asking the following: Can I use Dispatcher.CurrentDispatcher and Dispatcher.Invoke in windows service app. ? If not, can somebody direct me in the right way to solve my problem? Any advice will be appreciated, Uros
I was working on the tips offered above, and come across something I do not understand. If I use a System.Threading.Timer in windows service app, and inside it's event handler check the current thread with
Thread.ManagedThreadId
I get the same value if I check the current thread in theOnStart()
method. If I do the same in a Windows forms or WPF app. I get different values. Does anybody knows what is going on? Thanks in advance, Uros -
The problem I am having is that the thread that created the timer and started it, is no longer available when the timer's interval elapses. When I created an instance of the CustomTimer - which is just a wrapper around System.Threading.Timer - I save the Dispatcher.CurrentDispatcher in the private field of the CustomTimer. Than I start the timer end wait for its event to get raised(still inside the CustomTimer) and then use the saved Dispatcher to marshal to the thread that created the timer. But in some casses the Thread property of the stored Dispatcher has the state of stoped or WaitSleepJoin. But I am not sure what is the reason for that - maybe one should not use dispatcher in the windows service? I can put the code here if I was not clear. Thanks for your time, Uros
But what's the problem? Why do you care? What are you trying to accomplish? Why would your code care what thread is being used? The only time I've cared about the thread is in WinForms applications, but a Windows Service has no GUI so I don't see what the problem is.
-
But what's the problem? Why do you care? What are you trying to accomplish? Why would your code care what thread is being used? The only time I've cared about the thread is in WinForms applications, but a Windows Service has no GUI so I don't see what the problem is.
-
I am just trying to avoid locking all the resources all over my app., which would be necessary if there would be multiple threads accesing them. Uros
But even with a System.Timers.Timer you still really only have one thread active at a time... unless your threads run longer than the timer interval and you wind up with overlapping threads -- but that doesn't really seem like a desireable state so I avoid it. If you do desire overlapping threads, then I can see where you could have trouble and I have no answers for you. Good luck.
-
I was working on the tips offered above, and come across something I do not understand. If I use a System.Threading.Timer in windows service app, and inside it's event handler check the current thread with
Thread.ManagedThreadId
I get the same value if I check the current thread in theOnStart()
method. If I do the same in a Windows forms or WPF app. I get different values. Does anybody knows what is going on? Thanks in advance, UrosMsdn says, the timer handler method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system. And the threads in the managed thread pool are always background threads. Does this explain why you are not receiving a callback on the UI thread when tried from WinForm or WF applications?
-
Msdn says, the timer handler method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system. And the threads in the managed thread pool are always background threads. Does this explain why you are not receiving a callback on the UI thread when tried from WinForm or WF applications?
Hi I know that timer tick event should execute on the thread from the thread pool. That is expected behavior and that does not bother me. The problem is that this is only true when used in desktop(WPF or Forms) app. If the timer is used in windows service app. it executes on the thread that created the timer instance (at least
Thread.CurrentThread.ManagedThreadId
implies so and that is what I do not understand. Thank you for your answer, Uros