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

前端 未结 8 860
深忆病人
深忆病人 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条回答
  •  遥遥无期
    2020-12-03 03:22

    @eFloh in the post marked as answer said:

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

    That is not what the docs say.
    You are using a System.Windows.Forms.Timer in your example code.
    That is a Forms.Timer.
    According to the C# docs the Timer events are raised on the UI thread.

    This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread ...

    Also see stackoverflow post here

提交回复
热议问题