How to asynchronously wait for x seconds and execute something then?

前端 未结 8 876
深忆病人
深忆病人 2020-12-03 03:03

I know there is Thread.Sleep and System.Windows.Forms.Timer and Monitor.Wait in C# and Windows Forms. I just can\'t seem to be able to figure out how to wait fo

8条回答
  •  猫巷女王i
    2020-12-03 03:36

    (transcribed from Ben as comment)

    just use System.Windows.Forms.Timer. Set the timer for 5 seconds, and handle the Tick event. When the event fires, do the thing.

    ...and disable the timer (IsEnabled=false) before doing your work in oder to suppress a second.

    The Tick event may be executed on another thread that cannot modify your gui, you can catch this:

    private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    
        private void StartAsyncTimedWork()
        {
            myTimer.Interval = 5000;
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Start();
        }
    
        private void myTimer_Tick(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                /* Not on UI thread, reenter there... */
                this.BeginInvoke(new EventHandler(myTimer_Tick), sender, e);
            }
            else
            {
                lock (myTimer)
                {
                    /* only work when this is no reentry while we are already working */
                    if (this.myTimer.Enabled)
                    {
                        this.myTimer.Stop();
                        this.doMyDelayedWork();
                        this.myTimer.Start(); /* optionally restart for periodic work */
                    }
                }
            }
        }
    

    Just for completeness: with async/await, one can delay execute something very easy (one shot, never repeat the invocation):

    private async Task delayedWork()
    {
        await Task.Delay(5000);
        this.doMyDelayedWork();
    }
    
    //This could be a button click event handler or the like */
    private void StartAsyncTimedWork()
    {
        Task ignoredAwaitableResult = this.delayedWork();
    }
    

    For more, see "async and await" in MSDN.

提交回复
热议问题