Wait one second in running program

前端 未结 10 1204
暗喜
暗喜 2020-12-08 12:30
dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
System.Threading.Thread.Sleep(1000);

İ want to wait one second before

相关标签:
10条回答
  • 2020-12-08 13:13

    Try this function

    public void Wait(int time) 
    {           
        Thread thread = new Thread(delegate()
        {   
            System.Threading.Thread.Sleep(time);
        });
        thread.Start();
        while (thread.IsAlive)
        Application.DoEvents();
    }
    

    Call function

    Wait(1000); // Wait for 1000ms = 1s
    
    0 讨论(0)
  • 2020-12-08 13:15

    Personally I think Thread.Sleep is a poor implementation. It locks the UI etc. I personally like timer implementations since it waits then fires.

    Usage: DelayFactory.DelayAction(500, new Action(() => { this.RunAction(); }));

    //Note Forms.Timer and Timer() have similar implementations. 
    
    public static void DelayAction(int millisecond, Action action)
    {
        var timer = new DispatcherTimer();
        timer.Tick += delegate
    
        {
            action.Invoke();
            timer.Stop();
        };
    
        timer.Interval = TimeSpan.FromMilliseconds(millisecond);
        timer.Start();
    }
    
    0 讨论(0)
  • 2020-12-08 13:21

    use dataGridView1.Refresh(); :)

    0 讨论(0)
  • 2020-12-08 13:24

    Is it pausing, but you don't see your red color appear in the cell? Try this:

    dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
    dataGridView1.Refresh();
    System.Threading.Thread.Sleep(1000);
    
    0 讨论(0)
提交回复
热议问题