Is it possible to invoke on a waiting thread?
I have a situation where I would like to have the main thread waiting, while other threads can invoke on the main thread, without calling Application.Run.
The following code shows what I try to achieve, except that the main t开发者_开发百科hread and the data loading thread are causing a dead lock.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = new Form1();
form1.Show();
Thread loadDataThread = new Thread(() =>
{
//Load Data that takes a long time
string title = LoadData();
form1.Invoke((Action)delegate { form1.Text = title; });
});
loadDataThread.Name = "Data Loading Thread";
loadDataThread.Start();
//wait for loading to be completed (deadlock)
loadDataThread.Join();
//Do more stuffs
Application.Run();
}
private static string LoadData()
{
Thread.Sleep(1000);
return "Hello Thread";
}
}
Thank you
Invoke only works because in a Windows Forms application there is a message loop running for the entire lifetime of the application. If you are blocked on Thread.Join, then the message loop is not processing messages which also means it's not processing your Invoke calls.
There are some blocking calls in the .NET framework that do pump messages but you can't (and shouldn't) rely on these because more often than not they create re-entrancy bugs in your application that are a huge pain to resolve.
Instead of blocking the UI thread while you're waiting for data to load, you should just let it run in the background with your splash screen showing. Then at the end you can use BeginInvoke to close the splash screen and open the main screen or whatever it is you need to do at the end of the loading. Something like...
static void Main() {
Application.EnableVisualStyles();
var splashScreen = new Form1();
var mainScreen = new Form2();
splashScreen.Show();
ThreadPool.QueueUserWorkItem(delegate {
LoadLotsOfData();
splashScreen.BeginInvoke(delegate {
splashScreen.Close();
mainScreen.Show();
});
}
Application.Run();
}
No. You need to call Application.Run
, and then schedule delegates on the UI thread.
P.S. It's a better idea to use the Task
class rather than Control.Invoke
.
精彩评论