How to start a method after a certain time interval?

Understood: for example, imagine a button in my form. When the user clicks on the button, some void method should start after 30 seconds.

There would be an empty DoAfterDelay method that takes two input parameters. The first method is the method (using delegates), and the second is the time interval. So, I will have:

 public delegate void IVoidDelegate(); static void DoAfterDelay(IVoidDelegate TheMethod, TimeSpan Interval) { // *** Some code that will pause the process for "Interval". TheMethod(); } 

So, I just need a piece of code to pause the process for a certain amount of time. Before that, I used this code for this:

 System.Threading.Thread.Sleep(Interval); 

But this code does not suit me, because it stops the whole process and stops the program. I do not want the program stuck in the DoAfterDelay method. This is why Thread.Sleep useless.

So can anyone suggest a better way? Of course, I searched about this, but most of the solutions I found were based on using a timer (like here, for example). But using a timer is my last opinion, because the method should be run once, and using timers makes the program incomprehensible to read. Therefore, I am looking for the best solution, if any. Or maybe I should use timers?

I think I should play with themes, but not sure. So I wonder if anyone can lead me to a solution. Thanks in advance.

+13
multithreading c # timer winforms
source share
7 answers

Can you use the task?

  Task.Factory.StartNew(() => { System.Threading.Thread.Sleep(Interval); TheMethod(); }); 
+10
source share

Here you can use the async function to wait .Net 4.5

You can use Task.Delay and give a delay in milliseconds. This is a very clean way. eg:

  private async void button1_Click(object sender, EventArgs e) { await Task.Delay(5000); TheMethod(); } 
+13
source share

DoAfterDelay starts a timer that starts only once, when it expires, it calls your "TheMethod" function. Why would it be dirty?

0
source share

Here is what you want:

  public static void Example1c() { Action action = DoSomethingCool; TimeSpan span = new TimeSpan(0, 0, 0, 5); ThreadStart start = delegate { RunAfterTimespan(action, span); }; Thread t4 = new Thread(start); t4.Start(); MessageBox.Show("Thread has been launched"); } public static void RunAfterTimespan(Action action, TimeSpan span) { Thread.Sleep(span); action(); } private static void DoSomethingCool() { MessageBox.Show("I'm doing something cool"); } 

One of the benefits of using Action is that it can be easily changed to pass parameters. Suppose you want to pass an integer to a DoSomethingCool. Just change it like this:

  public static void Example1c() { Action<int> action = DoSomethingCool; TimeSpan span = new TimeSpan(0, 0, 0, 5); int number = 10; ThreadStart start = delegate { RunAfterTimespan(action, span, number); }; Thread t4 = new Thread(start); t4.Start(); MessageBox.Show("Thread has been launched"); } public static void RunAfterTimespan(Action<int> action, TimeSpan span, int number) { Thread.Sleep(span); action(number); } private static void DoSomethingCool(int number) { MessageBox.Show("I'm doing something cool"); } 

Very flexible ...

0
source share

There are several ways to create threads, but of course it depends on what you do. You can create a stream on the fly like this:

 Thread aNewThread = new Thread( () => OnGoingFunction() ); aNewThread.Start(); 

This thread will run in the background. The function you want to do must have a sleep method for sleeping when it is processed. So something like this:

 private void OnGoingFunction() { //Code.... Thread.Sleep(100); //100 ms, this is in the thead so it will not stop your winForm //More code.... } 

I hope this helps.

Another option is to create a thread whenever you need to process it, and not worry about the sleep option. Just create a new thread every time to load the process.

0
source share

You have to create a Coroutine

 public IEnumerator waitAndRun() { // WAIT FOR 3 SEC yield return new WaitForSeconds(3); // RUN YOUR CODE HERE ... } 

And name it with:

 StartCoroutine(waitAndRun()); 
0
source share

You can specify exact seconds using

 DateTime runTime = new DateTime(); double waitSeconds = (runTime - DateTime.Now).TotalSeconds; Task.Factory.StartNew(() => { Thread.Sleep(TimeSpan.FromSeconds(waitSeconds)); YourMethod(); }); 

runTime => When you want to execute a method.

0
source share

All Articles