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. Stop all threads in Threading.Timer in Callback method?

Stop all threads in Threading.Timer in Callback method?

Scheduled Pinned Locked Moved C#
questioncareer
5 Posts 3 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.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job? System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } } If the above is not safe enough, how should I do it? Thanks!

    A realJSOPR 2 Replies Last reply
    0
    • X xkrja

      As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job? System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } } If the above is not safe enough, how should I do it? Thanks!

      A Offline
      A Offline
      Alan N
      wrote on last edited by
      #2

      System.Threading.Timer timer;
      private void StartThreadTimer()
      {
      timer = new System.Threading.Timer(TimerCallback, "hepp", 5000, Timeout.Infinite);
      }

      void TimerCallback(object callbackObj)
      {
      Thread.Sleep(1000);
      if (!success) {
      // restart
      timer.Change(5000, Timeout.Infinite);
      }
      }

      I would use a single shot timer and restart it in the callback if the task is not successful. This way the callback can never be reentered and the lock isn't needed. Alan.

      1 Reply Last reply
      0
      • X xkrja

        As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job? System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } } If the above is not safe enough, how should I do it? Thanks!

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        Be a man, and use threads instead. Timers are for people that are closet VB programmers.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

        X 1 Reply Last reply
        0
        • realJSOPR realJSOP

          Be a man, and use threads instead. Timers are for people that are closet VB programmers.

          ".45 ACP - because shooting twice is just silly" - JSOP, 2010
          -----
          You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

          X Offline
          X Offline
          xkrja
          wrote on last edited by
          #4

          I'm just curious: How would you achieve what I'm after with a separate thread? I like the idea but they're scary :-) Would I just use Thread.Sleep in that thread instead of a timer? Thanks!

          realJSOPR 1 Reply Last reply
          0
          • X xkrja

            I'm just curious: How would you achieve what I'm after with a separate thread? I like the idea but they're scary :-) Would I just use Thread.Sleep in that thread instead of a timer? Thanks!

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            Using threads requires more code, but they're a lot more flexible than timers. That, and the fact that Toimer events in Windows are the lowest priority events, and on a busy system, they're not guaranteed to be fired. That means there's a certain lack of reliability, and that's a "Bad Thing" (TM). Before committing to using threads in a production app, you should create some test apps and get used to the idea of them. There are a lot of good articles reagarding the use of multithreading here on CodeProject.

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997

            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