3

c# how to pause between 2 function calls without stopping main thread

Foo();
Foo(); // i want this to run after 2 min without stopping main thread


Function Foo()
{
}

Thanks

user829174
  • 6,132
  • 24
  • 75
  • 125
  • 2
    use a timer, or a seperate thread for both and thread.sleep – x4rf41 Apr 18 '13 at 15:15
  • 1
    see this, an explanation why timer is efficient than stopping the thread.. http://stackoverflow.com/questions/391621/compare-using-thread-sleep-and-timer-for-delayed-execution – mdcuesta Apr 18 '13 at 15:17

6 Answers6

2

Try:

Task.Factory.StartNew(() => { foo(); })
    .ContinueWith(t => Thread.Sleep(2 * 60 * 1000))
    .ContinueWith(t => { Foo() });
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Zoran Causev
  • 360
  • 1
  • 6
  • This will waste a thread from pool to just sleep on it for 2 minutes(!). Pool is something limited and it could execute a lot of work on that thread, instead of being busy doing nothing – illegal-immigrant Apr 18 '13 at 15:43
2
    Task.Factory.StartNew(Foo)
                .ContinueWith(t => Task.Delay(TimeSpan.FromMinutes(2)))
                .ContinueWith(t => Foo());

Please, do not sleep on thread pool. Never

"There are only a limited number of threads in the thread pool; thread pools are designed to efficiently execute a large number of short tasks. They rely on each task finishing quickly, so that the thread can return to the pool and be used for the next task." More here

Why Delay? It uses DelayPromise internally with a Timer, it's efficient, way more efficient

Community
  • 1
  • 1
illegal-immigrant
  • 8,089
  • 9
  • 51
  • 84
1

How about using a Timer:

var timer = new Timer();
timer.Interval = 120000;
timer.Tick += (s, e) =>
{
    Foo();
    timer.Stop();
}
timer.Start();
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1

Try spawning a new thread, like so:

new Thread(() => 
    {
         Foo();
         Thread.Sleep(2 * 60 * 1000);
         Foo();
    }).Start();
slawekwin
  • 6,270
  • 1
  • 44
  • 57
  • If the OP is using .NET 4.0 or later, it's better to use `Task`. – MBender Apr 18 '13 at 15:20
  • 1
    In such a simple case, I would back the claim that `Task` is better with reasons. – Zdeslav Vojkovic Apr 18 '13 at 15:36
  • This is actually better than using Task+Sleep, this solution uses dedicated thread and does not steal threads from pool for making important stuff like sleeping...But, Task+Delay is way more easier – illegal-immigrant Apr 18 '13 at 15:41
  • @taras.roshko that's why I ask. I see too often that Task is used without rethinking the context or to execute long running actions – Zdeslav Vojkovic Apr 18 '13 at 15:45
  • 1
    @ZdeslavVojkovic agree. It's always better to know the internals – illegal-immigrant Apr 18 '13 at 15:52
  • @taras.roshko I would say that you should never use Thread.Sleep to pause for 2 minutes. Not in the first or the second case. What if you want to cancel thread execution? Should the user wait for 2 minutes to expire the wait time? A better solution is to use an EventWaitHandle or some other wait mechanism that you can cancel at any time. – Zoran Causev Apr 18 '13 at 17:42
  • @ZoranCausev I completely agree with you, nevertheless I believe that main part of the question was not about which delay method should be used in this case (also that 2 minutes was just an example) – slawekwin Apr 19 '13 at 05:54
0

You can use a Timer Class.

using System;
using System.Timers;

public class Timer1
{
    private static System.Timers.Timer aTimer;

    public void Foo()
    {
    }

    public static void Main()
    {
        Foo();

        // Create a timer with a two minutes interval.
        aTimer = new System.Timers.Timer(120000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(Foo());

        aTimer.Enabled = true;
    }

    // Specify what you want to happen when the Elapsed event is  
    // raised. 
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Foo();
    }
}

The code has not been tested.

misleadingTitle
  • 657
  • 6
  • 21
0
var testtask = Task.Factory.StartNew(async () =>
    {
        Foo();
        await Task.Delay(new TimeSpan(0,0,20));
        Foo();
    });
rclement
  • 1,664
  • 14
  • 10