---
# System prepended metadata

title: '[C# WinForm] - Use Thread.Sleep() and Task.Delay() to observe sync and async behavior'

---

GitHub: [https://github.com/CarterWu-M/C-WinForms_sync-async]


This example code uses a text box and a button to observe a timer, setting a value in the text box every second. It also includes Thread.Sleep() and Task.Delay() for observations during the delay periods.
![image](https://hackmd.io/_uploads/ByHK2pDxyg.png)

---
### Thread.Sleep()
---
```C#=
namespace sync_async
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Timer _timer;
        private uint _elapsedSec = 0;
        
        public Form1()
        {
            InitializeComponent();

            this._timer = new System.Windows.Forms.Timer();
            this._timer.Interval = 1000;
            this._timer.Tick += Timer_tick;
        }
        
        private void Timer_tick(object sender, EventArgs e)
        {
            this._elapsedSec++;
            TimeSpan timeSpan = TimeSpan.FromSeconds(this._elapsedSec);

            txt1.Text = timeSpan.ToString(@"ss");
            txt1.Refresh();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            txt1.Text = "Waiting for 5 seconds...";
            txt1.Refresh();
            this._elapsedSec = 0;
            this._timer.Start();

            //sync method, blocking UI
            Thread.Sleep(5000);

            this._timer.Stop();
            txt1.Text = "Wait completed!";
            txt1.Refresh();
        }
    }
}
```

![image](https://hackmd.io/_uploads/rJXihaDgyx.png)
=> UI is blocked for 5 seconds
![image](https://hackmd.io/_uploads/SyL226Dg1e.png)
You will find that the string only displays 'Waiting for 5 seconds...' and 'Wait completed!' You won't see the timer value '00', '01', '02'...for each second.

Because Thread.sleep() is a synchronous method, it will block the UI, preventing you from seeing any timer values on the screen.
![image](https://hackmd.io/_uploads/rJhQBUqxkg.png)

---
### Task.Delay()
---
```C#=
private void button1_Click(object sender, EventArgs e)
{
    txt1.Text = "Waiting for 5 seconds...";
    txt1.Refresh();
    this._elapsedSec = 0;
    this._timer.Start();

    //async method, non-blocking UI
    Task.Delay(5000);

    this._timer.Stop();
    txt1.Text = "Wait completed!";
    txt1.Refresh();
}
```
![image](https://hackmd.io/_uploads/rJXihaDgyx.png)
=> change immediately
![image](https://hackmd.io/_uploads/SyL226Dg1e.png)
you will see the string changed from 'Waiting for 5 seconds...' to 'Wait completed!' after a short delay.

Because Task.Delay() is an asynchronous method, it won't blok the UI thread, allowing the code to continue to the next line.
![image](https://hackmd.io/_uploads/SkS2P8qxkg.png)

---
### Await Task.Delay()
---
```C#=
private async void button1_Click(object sender, EventArgs e)
{
    txt1.Text = "Waiting for 5 seconds...";
    txt1.Refresh();
    this._elapsedSec = 0;
    this._timer.Start();

    //async method, non-blocking UI
    await Task.Delay(5000);

    this._timer.Stop();
    txt1.Text = "Wait completed!";
    txt1.Refresh();
}
```
![image](https://hackmd.io/_uploads/rJXihaDgyx.png)
![image](https://hackmd.io/_uploads/Skf8w0Pxkg.png)
![image](https://hackmd.io/_uploads/HygTwCPgke.png)
![image](https://hackmd.io/_uploads/ByW0DRvlye.png)
![image](https://hackmd.io/_uploads/ryL1OAPekg.png)
![image](https://hackmd.io/_uploads/SyL226Dg1e.png)
Now you can see the timer value in the text box each second because 'await' will wait for Task.Delay() to complete before moving to the next line of code. Since Task.Delay() is an asynchronous method, it won't block the UI thread, allowing the timer to update the value in the text box.

When you use the 'await' syntax, remember to add the 'async' keyword before the function name.
![image](https://hackmd.io/_uploads/HJQWD8cl1g.png)



