Wait for n seconds, then next line of code without freezing form

前端 未结 4 1540
长发绾君心
长发绾君心 2020-12-15 20:45

Hi I am trying to find a method of waiting a number of milliseconds before moving to the next line of code,

I have looked into Thread.Sleep but this will freeze the

4条回答
  •  猫巷女王i
    2020-12-15 21:23

    Try using a DispatcherTimer. It's a pretty handy object that does all the work of delegating to the UI thread.

    For example:

    private DispatcherTimer _dtTimer = null;
    
    public Constructor1(){
      _dtTimer = new DispatcherTimer();
      _dtTimer.Tick += new System.EventHandler(HandleTick);
      _dtTimer.Interval = new TimeSpan(0, 0, 0, 2); //Timespan of 2 seconds
      _dtTimer.Start();
    }
    
    private void HandleTick(object sender, System.EventArgs e) {
      _uiTextBlock.Text = "Timer ticked!";
    }
    

提交回复
热议问题