Can someone explain why invoking Thread.Sleep
from BackgroundWorker
blocks its execution. Invoke should cause the delegate to be executed on the UI thread and background thread should continue with execution. But that does not happen - why?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BackgroundWorker bgrw = new BackgroundWorker();
bgrw.DoWork += new DoWorkEventHandler(bgrw_DoWork);
bgrw.RunWorkerAsync();
}
void bgrw_DoWork(object sender, DoWorkEventArgs e)
{
Console.WriteLine(DateTime.Now);
this.Invoke(new Action(() => { Thread.Sleep(2000); })); //should be executed on the UI thread
Console.WriteLine(DateTime.Now); // This line is executed after 2 seconds
}
}