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. threading and timercallback

threading and timercallback

Scheduled Pinned Locked Moved C#
helpquestion
9 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.
  • S Offline
    S Offline
    Saamir
    wrote on last edited by
    #1

    Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!

    L Y 2 Replies Last reply
    0
    • S Saamir

      Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, what do you want to happen when MonitorFunc hasn't finished in one minute: - drop one execution (i.e. ignore the new one that overlaps, so the next run is up to one minute after the current one finishes); - stop the running one and run the new one; - finish the running one, then immediately run the new one? You decide, and code accordingly! :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      S 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, what do you want to happen when MonitorFunc hasn't finished in one minute: - drop one execution (i.e. ignore the new one that overlaps, so the next run is up to one minute after the current one finishes); - stop the running one and run the new one; - finish the running one, then immediately run the new one? You decide, and code accordingly! :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        S Offline
        S Offline
        Saamir
        wrote on last edited by
        #3

        Hi Luc, I am not sure if I understand you. However, I want to finish running one, and then start the new one. Basically if monitorfunc has not finished I want to pause the call if one minute is passed. Then start the call once it has finished. I hope I am making sense. Please advice.

        Sameer

        L 1 Reply Last reply
        0
        • S Saamir

          Hi Luc, I am not sure if I understand you. However, I want to finish running one, and then start the new one. Basically if monitorfunc has not finished I want to pause the call if one minute is passed. Then start the call once it has finished. I hope I am making sense. Please advice.

          Sameer

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          OK, so if one run exceeds the allotted time period, you want the current run to terminate normally, and the new run of the function to follow immediately. This is some pseudo-code that would achieve this:

          private int activationCounter=0;

          void periodicFunction() {
          int n=Interlocked.Increment(ref activationCounter); // increments in a safe way
          if (n<=1) { // check no other execution is going on
          do {
          // whatever needs to be done periodically
          // (while this happens, the counter could get incremented)
          // when done, decrement and test the counter:
          n=Interlocked.Decrement(ref activationCounter); // decrements in a safe way
          } while (n!=0); // check more is required
          }

          :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          S 1 Reply Last reply
          0
          • S Saamir

            Please help. Here is a snippet of my code: using TD=System.Threading; TD.Timer m_tMonitor; m_tMonitor = new TD.Timer(new TD.TimerCallback(MonitorFunc), 1, 0, 60000); I am trying to monitor a schedule every minute, and if schedule is past due then I am performing some tasks. My problem is sometimes monitorfunc is not completed in 1 minute so timercallback is triggered again. Is there a way I can use something like WAIT for monitorfunc to complete what is is doing before I call it again. HELP!!!

            Y Offline
            Y Offline
            yunusdemiray
            wrote on last edited by
            #5

            bool stop = false;
            ...
            Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
            ...

            private void run(){
            while(!stop){
            Thread.Sleep(60000);
            MonitorFunc();
            }
            }

            S L 2 Replies Last reply
            0
            • L Luc Pattyn

              OK, so if one run exceeds the allotted time period, you want the current run to terminate normally, and the new run of the function to follow immediately. This is some pseudo-code that would achieve this:

              private int activationCounter=0;

              void periodicFunction() {
              int n=Interlocked.Increment(ref activationCounter); // increments in a safe way
              if (n<=1) { // check no other execution is going on
              do {
              // whatever needs to be done periodically
              // (while this happens, the counter could get incremented)
              // when done, decrement and test the counter:
              n=Interlocked.Decrement(ref activationCounter); // decrements in a safe way
              } while (n!=0); // check more is required
              }

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              S Offline
              S Offline
              Saamir
              wrote on last edited by
              #6

              Hi Luc, Sorry for the delay in my response and thank you for your responses. I couldn't implement your code, I am not sure if I am doing something wrong. I have put in another posting requesting guidance on building a service with a thread that looks at a folder for files and processes the file. Upon completion of the processing, it starts over again. Help if you can please on that.

              Sameer

              L 1 Reply Last reply
              0
              • Y yunusdemiray

                bool stop = false;
                ...
                Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
                ...

                private void run(){
                while(!stop){
                Thread.Sleep(60000);
                MonitorFunc();
                }
                }

                S Offline
                S Offline
                Saamir
                wrote on last edited by
                #7

                thank you for your response. I am using timercallback currently, sorry for my ignorance in threading but I am not sure how to modify all my current code to implement your method.

                Sameer

                1 Reply Last reply
                0
                • Y yunusdemiray

                  bool stop = false;
                  ...
                  Thread t = new Thread(new ThreadStart(run)){IsBackground = true}.Start();
                  ...

                  private void run(){
                  while(!stop){
                  Thread.Sleep(60000);
                  MonitorFunc();
                  }
                  }

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  That is not really what the OP wanted, since it will pause for an entire minute in between two executions, whereas the OP wanted it to run every minute. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • S Saamir

                    Hi Luc, Sorry for the delay in my response and thank you for your responses. I couldn't implement your code, I am not sure if I am doing something wrong. I have put in another posting requesting guidance on building a service with a thread that looks at a folder for files and processes the file. Upon completion of the processing, it starts over again. Help if you can please on that.

                    Sameer

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    Saamir wrote:

                    I couldn't implement your code

                    that is extremely vague and uninformative, no one can help you based on this. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                    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