Reputation: 1404
I am developing a chat application with C# and we are using System.Timer.Timer for frequently getting data for the new request and zone request.
It is running fine but whenever we create a new instance a cross threaded operation exception occurs.
This is my code:
frmchat f = new frmchat();
MethodInvoker method = new MethodInvoker(delegate()
{
f.Name = "xyz";f.Show();
});
if (f.InvokeRequired)
{
f.Invoke(method);
}
else
{
method();
}
This will create a number of chat forms.
Upvotes: 0
Views: 227
Reputation: 16162
If you are creating that form at the elapsed event handler of that timer then use this.Invoke
instead, like:
this.Invoke(new MethodInvoker(() =>
{
frmchat f = new frmchat();
f.Name = "xyz";
f.Show();
}
Upvotes: 0
Reputation: 13079
I'm guessing your problem is new forms can only be created on the UI thread. Try to use a Windows.Forms.Timer if possible. This will reinvoke the event on the correct thread automagicly for you.
Upvotes: 1