Wait one second in running program

前端 未结 10 1193
暗喜
暗喜 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:00

    Wait function using timers, no UI locks.

    public void wait(int milliseconds)
    {
        var timer1 = new System.Windows.Forms.Timer();
        if (milliseconds == 0 || milliseconds < 0) return;
    
        // Console.WriteLine("start wait timer");
        timer1.Interval = milliseconds;
        timer1.Enabled  = true;
        timer1.Start();
    
        timer1.Tick += (s, e) =>
        {
            timer1.Enabled = false;
            timer1.Stop();
            // Console.WriteLine("stop wait timer");
        };
    
        while (timer1.Enabled)
        {
            Application.DoEvents();
        }
    }
    

    Usage: just placing this inside your code that needs to wait:

    wait(1000); //wait one second
    
    0 讨论(0)
  • 2020-12-08 13:04

    Busy waiting won't be a severe drawback if it is short. In my case there was the need to give visual feedback to the user by flashing a control (it is a chart control that can be copied to clipboard, which changes its background for some milliseconds). It works fine this way:

    using System.Threading;
    ...
    Clipboard.SetImage(bm);   // some code
    distribution_chart.BackColor = Color.Gray;
    Application.DoEvents();   // ensure repaint, may be not needed
    Thread.Sleep(50);
    distribution_chart.BackColor = Color.OldLace;
    ....
    
    0 讨论(0)
  • 2020-12-08 13:05

    Maybe try this code:

    void wait (double x) {
        DateTime t = DateTime.Now;
        DateTime tf = DateTime.Now.AddSeconds(x);
    
        while (t < tf) {
            t = DateTime.Now;
        }
    }
    
    0 讨论(0)
  • 2020-12-08 13:06

    I feel like all that was wrong here was the order, Selçuklu wanted the app to wait for a second before filling in the grid, so the Sleep command should have come before the fill command.

        System.Threading.Thread.Sleep(1000);
        dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
    
    0 讨论(0)
  • 2020-12-08 13:08

    .Net Core seems to be missing the DispatcherTimer.

    If we are OK with using an async method, Task.Delay will meet our needs. This can also be useful if you want to wait inside of a for loop for rate-limiting reasons.

    public async Task DoTasks(List<Items> items)
    {
        foreach (var item in items)
        {
            await Task.Delay(2 * 1000);
            DoWork(item);
        }
    }
    

    You can await the completion of this method as follows:

    public async void TaskCaller(List<Item> items)
    {
        await DoTasks(items);
    }
    
    0 讨论(0)
  • 2020-12-08 13:10

    The Best way to wait without freezing your main thread is using the Task.Delay function.

    So your code will look like this

    var t = Task.Run(async delegate
    {              
        dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
        dataGridView1.Refresh();
        await Task.Delay(1000);             
    });
    
    0 讨论(0)
提交回复
热议问题