How to create reusable timer that delays program by varying amounts of time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mrcw
    New Member
    • Nov 2008
    • 82

    #1

    How to create reusable timer that delays program by varying amounts of time

    I'm trying to make a timer which is a reusable module. Is this possible? I would like to pass 1 argument to the timer. The argument is the delay that the main program waits before it continues.

    The main program would be something like

    start
    do something (process 1)
    pass argument ( delay time eg 2 sec) to timer
    after timer has timed out program continues
    do something (process 2)
    pass argument ( delay time eg 1 sec) to timer
    after timer has timed out program continues
    do something (process 3)
    pass argument ( delay time eg 4 sec) to timer
    after timer has timed out program continues
    do something (process 4)
    pass argument ( delay time eg 0.5 sec) to timer
    after timer has timed out program continues
    end

    As you can see 1 timer is used to provide repeatedly delays to the program by varying amounts of time. I cant stop the thread because other inputs are received when the timer has delayed the program and still have to be acted upon.

    Ive looked around but I cant find anything close to what Im trying to do.

    Any ideas or pointers?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    If the timer is delaying the program, why can't you use thread.sleep?
    You could make it smaller increments of thread.sleep in a loop and include Application.DoE vents() or something if you need signal handling?

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      So you want the timer class itself to do the work, then after an interval send a message to the main class and tell it to do something?

      I'm really not following your example, but there is a built in .NET Timer object that will tick after a preset time, triggering an event that you can handle. When this event is triggered you can just as easily stop the timer, then change the tick period and/or do something else.

      Sorry if I'm not reading your example properly... who is doing the work? The timer or the main program?

      Comment

      • mrcw
        New Member
        • Nov 2008
        • 82

        #4
        Application.DoE vents() .. I'n not that good a programmer yet!

        who is doing the work? Imagine that the processes are motors turning. They dont reach their target position immediately, but the program does. The program needs to be delayed while the motors catch up. If somebody hits the stop switch the motors still need to react to that. So I cant put thread to sleep.

        I eventually used

        Code:
        void timer0( float secs)
                {
                    float a = 0;
                    DateTime CurrTime = DateTime.Now;
                    CurrTime = CurrTime.AddSeconds(secs);
                    while (!(DateTime.Now.Second == CurrTime.Second))
                        a = +1;
                    return;
                }
        well, it makes sense to me.
        Thank you for your help

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You have effectively made a busy loop there.
          Not only will that stop up the thread, but it will also spike your resource usage.
          Adding in a Thread.Sleep(1) inside your loop will greatly aid that problem.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Can't you just use events to have the worker threads signal the main thread that they are done and it should proceed? I don't even know that you need a loop, unless you're continuously processing something and are just waiting for, say, a buffer to fill up again.

            Maybe have the main thread set up listeners to your worker threads which fire an event called WorkDone when they are finished. The main thread receives this event and processes whatever work was completed, then goes back to waiting.

            If your main thread is a gui (ie, form), it can just sit there in the open state and receive events. If it's a console program then yea, you'll need a thread.sleep loop.

            Would that help?

            Comment

            • mrcw
              New Member
              • Nov 2008
              • 82

              #7
              ok I thought I'd better look at threads...from the beginning. I've looked at sites and books but they all show complex examples.

              I have written a simple program a form with a textbox on it.
              The code for the form is here

              Code:
              using System;
              using System.Text;
              using System.Windows.Forms;
              using System.Threading;
              
              namespace threadtest6
              {
                  public partial class Form1 : Form
                  {
                      public Form1()
                      {
                          InitializeComponent();
                      }
                     
                      Thread thread1; 
                      
                      private void Form1_Load(object sender, EventArgs e)
                      {
                          a(); 
                      }
              
                      public void a()
                      {
                          thread1.Start();
                          textBox1.Text = "1";
                          thread1.Abort();
                      }
                  }
              }
              My first questions are 1) I get an error 'threadtest6.Fo rm1.thread1' is never assigned to, and will always have its default value null"

              How do I assign a value to a thread and remove the error?

              2) How do I put the text from a() into textbox1 on the form?

              can you recommend any good websites or books about c# threads (remember very simple).
              thank-you

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                Here you are, sir!


                The error message is pretty clear actually. References in C# initialize to null and you haven't initialized thread1. You can either do it before you call it, in the constructor, or at it's declaration.

                The simplest way might be...

                Code:
                Thread thread1 = new Thread(<method to run>);
                As for the text... you've modified a property on a control that belongs to the form itself. It should already be on the form. Though really, I'm actually not sure what you're doing there... you start a thread, change text, then immediately stop the thread.

                Have a read through that e-book, hopefully it'll help you out ;)

                Comment

                • mrcw
                  New Member
                  • Nov 2008
                  • 82

                  #9
                  Thank-you

                  A weekend of reading approaches. But I have a feeling i will have more questions1

                  Comment

                  Working...