Hi,
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 thread 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