Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Windows service & Timers

Windows service & Timers

Scheduled Pinned Locked Moved C#
helpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    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

    P D K 3 Replies Last reply
    0
    • K koleraba

      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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      I use System.Timers.Timer for my Windows Services and I have no trouble. What problem are you having?

      K 1 Reply Last reply
      0
      • K koleraba

        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

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Have a look at the AsyncOperation and AsyncOperationManager classes. The former has a Post and PostOperationCompleted methods that will call a method on the required thread via the SendOrPostCallback 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)

        K 1 Reply Last reply
        0
        • P PIEBALDconsult

          I use System.Timers.Timer for my Windows Services and I have no trouble. What problem are you having?

          K Offline
          K Offline
          koleraba
          wrote on last edited by
          #4

          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

          P 1 Reply Last reply
          0
          • D DaveyM69

            Have a look at the AsyncOperation and AsyncOperationManager classes. The former has a Post and PostOperationCompleted methods that will call a method on the required thread via the SendOrPostCallback 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)

            K Offline
            K Offline
            koleraba
            wrote on last edited by
            #5

            Thanks for your suggestions. I will try to do some more researh and see if it helps. Uros

            1 Reply Last reply
            0
            • K koleraba

              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

              K Offline
              K Offline
              koleraba
              wrote on last edited by
              #6

              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 the OnStart() 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

              2 1 Reply Last reply
              0
              • K koleraba

                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

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                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.

                K 1 Reply Last reply
                0
                • P PIEBALDconsult

                  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.

                  K Offline
                  K Offline
                  koleraba
                  wrote on last edited by
                  #8

                  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

                  P 1 Reply Last reply
                  0
                  • K koleraba

                    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

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • K koleraba

                      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 the OnStart() 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

                      2 Offline
                      2 Offline
                      224917
                      wrote on last edited by
                      #10

                      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?

                      K 1 Reply Last reply
                      0
                      • 2 224917

                        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?

                        K Offline
                        K Offline
                        koleraba
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups