Selçuklu Ebrar
Selçuklu Ebrar

Reputation: 2279

Wait one second in running program

dataGridView1.Rows[x1].Cells[y1].Style.BackColor = System.Drawing.Color.Red;
System.Threading.Thread.Sleep(1000);

İ want to wait one second before printing my grid cells with this code, but it isn't working. What can i do?

Upvotes: 198

Views: 791838

Answers (13)

Adam Cvikl
Adam Cvikl

Reputation: 21

Wait for 1 second

Task.Delay(1000).Wait();

Upvotes: 2

Colin D
Colin D

Reputation: 99

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;

Upvotes: 9

Rich Hildebrand
Rich Hildebrand

Reputation: 1885

.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);
}

Upvotes: 35

v.t.
v.t.

Reputation: 111

If using async

await Task.Delay(new TimeSpan(0, 0, 1));

else

Thread.Sleep(new TimeSpan(0, 0, 1));

Upvotes: 10

Dave
Dave

Reputation: 3311

This solution is short and, so far as I know, light and easy.

public void safeWait(int milliseconds)
{
    long tickStop = Environment.TickCount + milliseconds;
    while (Environment.TickCount < tickStop)
    {
        Application.DoEvents();
    }
}

Upvotes: 1

amitklein
amitklein

Reputation: 1395

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);             
});

Upvotes: 11

Said
Said

Reputation: 493

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

Upvotes: 36

Bartek Winslawski
Bartek Winslawski

Reputation: 35

Maybe try this code:

void wait (double x) {
    DateTime t = DateTime.Now;
    DateTime tf = DateTime.Now.AddSeconds(x);

    while (t < tf) {
        t = DateTime.Now;
    }
}

Upvotes: -4

Savaş SERTER
Savaş SERTER

Reputation: 11

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

Upvotes: 1

ChrisW
ChrisW

Reputation: 39

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;
....

Upvotes: 3

Mark Rowe
Mark Rowe

Reputation: 999

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();
}

Upvotes: 54

Coder SAJDFJF
Coder SAJDFJF

Reputation: 25

use dataGridView1.Refresh(); :)

Upvotes: 0

Matt Dawdy
Matt Dawdy

Reputation: 19717

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);

Upvotes: 300

Related Questions