Hey.
I'm having a problem.
I have an event, that I subscribe to from a WinForm.
When I call the event from another thread, it fails, since it cannot access the winforms thread.
So, how do I invoke an event, so that everyone thats subscribed to it gets it invoked, so it works across threads?
-
-
Let's see some code & your error messages.
-
Not really easy to paste some code, since it's really complicated.
But the Form subscribes to an event, and the event is fired from another thread.
So the question really is: "How do I fire an event, if some of the classes that has subscribed to it is Forms, so it doesn't throw a cross thread execption?" -
Sounds like you're using .Net 2.0 --- which looks out for you by throwing an exception when you're trying to manipulate a UI element from a different thread other than the UI thread.
I haven't played much with .Net 2.0, so my solution is going to be 1.0-ish.
What you can do is -- your form has an Invoke method, so just call that from your event and you should be OK. -
Pretty sure all he needs is a publicly accessable delegate to pass the events off to the UI.
If he is using a BGWorker, you can pretty easily pass the events around.
EDIT: something Like this (not tested)
//almost forgot.
//In your threaded class
public event MyThreadedClass.MyCompletedEventHandler ItsCompleted ;
public delegate void MyCompletedEventHandler(object sender, RunWorkerCompletedEventArgs e); //Delegate
//In UI Class
MyThreadedClass.ItsCompleted += new MyCompletedEventHandler(frmMain_SomethingCompleted);
void frmMain_SomethingCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
//I think thats right
-
or the simplest possible way

// the method called by other thread, this method is in UIs thread
private void OnSomething(object sender, System.SomeEventArgs e)
{
if (this.InvokeRequired)
Invoke(new MethodInvoker(delegate() { OnSomething(sender, e); }));// your code here
} -
Actually, no.phreaks wrote://I think thats right

This would only work if your event was raised by a BackgroundWorker component.
Otherwise, you'll have to do something like what Ion suggests.
-
Minh wrote:
Actually, no.
phreaks wrote: //I think thats right

This would only work if your event was raised by a BackgroundWorker component.
Otherwise, you'll have to do something like what Ion suggests.
That's why I said, "If he is using a BGWorker,"...
-
hey, something strange just happened, don't understand why,
if you run (F5) this code inside Visual Studio, you get an "InvalidOperationException", Cross Thread ...:
public partial class Form1 : Form
{
System.Timers.Timer timer = new System.Timers.Timer(3000);public Form1()
{
InitializeComponent();timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
textBox1.Text = "sd";
}
}
but if you run the generated assembly from Debug or Release folder exception does not occur, why? anyone
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.