Reputation: 843
In my program.cs file, the code is as follows:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmWizard frm = new frmWizard();
Application.Run(frm);
Thread th = new Thread(frm.ThreadSetCom);
th.Start();
}
ThreadSetCom is a method that runs in an endless loop checking for something. I noticed that ThreadSetCom will only execute just before the WinForm appears and after i close the form. It doesn't execute while the Form is visible. Can anyone clear this up to me please?
Upvotes: 1
Views: 880
Reputation: 317
Application.Run will block until you have closed the form which explains why you see this behaviour. Adil's answer will work but I believe you shouldn't be coupling your code in this way. It would be better if your Main method started up the second thread independantly of the Form Load event.
So you just need to re-arrange your code like this:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frmWizard frm = new frmWizard();
Thread th = new Thread(frm.ThreadSetCom);
th.Start();
Application.Run(frm);
}
Upvotes: 1
Reputation: 148178
Application.Run waits until the passed form in argument is closed. You may need to create
and start
the thread
in the load
event of the frmWizard.
private void frmWizard_Load(object sender, System.EventArgs e)
{
Thread th = new Thread(ThreadSetCom);
th.Start();
}
Upvotes: 2