Reputation: 1486
I'm trying to write a chat client in C# and have run into a problem.
How it works is that the client polls the server every 1 second to see if there are any new actions to take (for example display a message in a channel, or whatever). The polling is done in a thread of its own.
Now, I want the polling thread to open a new MDI form when a channel open action is received (meaning the user has entered a new channel). The thing is, the new form should run on the MAIN program thread, and not on the worker thread.
So basically I'm asking, how do I create a new windows form, and associate it with an already existing thread? (instead of the thread that created it).
Also, if you know of a better way to do this, please tell me. I love improving my program architecture!
Thanks all
Upvotes: 2
Views: 675
Reputation: 26468
You should invoke the function from your main window:
void MyWorkerThread() {
while (Connected) {
Thread.Sleep(1000);
if (NewMessage) {
ShowNewForm();
}
}
}
void ShowNewForm() {
if (this.InvokeRequired) { // this refers to the current form
this.Invoke(new Action(ShowNewForm)); // this line invokes the same function on the same thread as the current form
return;
}
Form myMdiForm = new Form();
myMdiForm.Show();
}
Upvotes: 0
Reputation: 1500065
Make the polling thread call back to the main form using Control.Invoke or Control.BeginInvoke, and create the form in the callback.
I assume you're already using Invoke/BeginInvoke to update the UI - this is just another kind of UI operation.
Upvotes: 4