3

I have a method that I want to call a method (will mention as myFanc) in seperated thread every 3 seconds

The code below can easly do it,

 Timer myTimer = new Timer();
 myTimer.Elapsed += new ElapsedEventHandler( myFanc );
 myTimer.Interval = 3000;
 myTimer.Start();

The code above may cause myFanc to be called while another call to myFanc isn't finished yet

My problem is that I also want myFanc to finished before I will call her agian, so basically I want to call the method in seperated thread every 3 seconds after myFanc is finished, how can I do it?

I don't mind if the solution won't use Timer class, I just want this behavior to work..

6 Answers 6

6

Set the AutoReset property of your timer to false, then, at the end of your event handler (i.e. "myFanc"), call the Start method of your timer again.

Sign up to request clarification or add additional context in comments.

Comments

2

At the top of myFunc put this

myTimer.Enabled = false;

and at the end when it is finished put this

myTimer.Enabled = true;

It will cause that you will temporairly disable the Timer while function is executing

Comments

1

Do not use a timer, loop in the thread calling "myfanc" and use

Thread.Sleep(3000);

to insert the desired delay.

6 Comments

@Delashmate: if you are indeed using different threads already, I second this suggestion - sleeping the second thread is the preferred way of handling the delay then.
@Delashmate: On what reasoning? This is an appropriate use of thread.sleep
@Jim, is Thread.Sleep consuming cpu? if so then it isn't elegant solution
Thread.Sleep suspend the thread, thus it does not consume CPU
@Delashmate event has nothing to do with Thread.Sleep. You can check that by suspending a thread for say, ten seconds ( 10000 ) the CPU comsumption of your process is 0...
|
1

This is the solution I used - thanks to Jim

using System;
using System.Timers;
using System.Threading;

class myApp
{
    public static void Main()
{
      System.Timers.Timer myTimer = new System.Timers.Timer();
      myTimer.Elapsed += new ElapsedEventHandler( myFanc );
      myTimer.Interval = 1000;
      myTimer.AutoReset = false;
      myTimer.Start();

      while ( Console.Read() != 'q' )
      {
          ;    // do nothing...
      }
    }

public static void myFanc(object source, ElapsedEventArgs e)
{
    Console.Write("\r{0}", DateTime.Now);

    Thread.Sleep(3000); //the sleep here is just to test the method, wait to be finished before another call the myFanc method is being performed
    System.Timers.Timer myTimer = new System.Timers.Timer();
    myTimer.Elapsed += new ElapsedEventHandler(myFanc);
    myTimer.Interval = 1000;
    myTimer.AutoReset = false;
    myTimer.Start();

}

}

Comments

0

2 methods than I can think of right now

  1. Stop and start the timer inside the myFanc method This ensures 3 seconds between each run
  2. Wrap your code in myFanc inside a lock, this will just skip a turn if it's not finished.

Make sure you use the correct timer.. system.threading.timer is a seperate thread. The timer class in windows.forms (or something like that) isn't

Comments

0

Use a thread and Thread.Sleep(x) like so

Thread thread = new Thread(new ThreadStart(myTimer));
thread.Start();


void myTimer()
{
    while (!exit)
    {
        myFunc();
        Thread.Sleep(3000);
    }
}

1 Comment

why? Its perfectly suited to introducing lag like as required here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.