BramVandenbon
Nov 21st, 2006, 12:50 PM
In short:
I am getting an error because I am trying to access a control from an eventhandler raised by a Thread.
Longer explanation:
Hi, this is a small piece of code of a class that does some heavy calculations in a seperate thread and raises events each time new results are available.
public abstract class MyClass
{
public event EventHandler NewResults;
private int intResult;
public void DoStuff()
{
Thread th = new Thread(new ThreadStart(Calculate));
th.Priority = ThreadPriority.Lowest;
th.Start();
}
private void Calculate()
{
while(true){
//...
//calculations on intResult here
//...
OnNewResult(this);
}
}
public int Result
{
get { return intResult; }
}
protected void OnNewResults(object sender)
{
if (NewResults != null)
NewResults(sender, new EventArgs());
}
}
(My real code is a lot more complicated. It is actually an encryption algorithm I wrote and the event will be used to update a progressbar.)
Is the way I programmed the raising of the event inside the thread ok? I have the feeling I am doing something wrong here.
The exception however is not thrown in the above code though. I get it in the following part of code, where I handle the event.
/* somewhere in the constructor I put
*
* MyClass c = new MyClass();
* c.NewResults += new EventHandler(NewResults);
* c.DoStuff();
/*
void NewResults(object sender, EventArgs e)
{
MyClass c = (MyClass)sender;
txtTextBox.Text = c.Result.ToString();
}
This gives me a "cross-thread operation error because the txtTextBox was created in another thread".
I am sure that this error most be something that also other people were confronted with. What is the cleanest and usual way to solve this?
Some ideas
I read something about using the Control.Invoke(new InvokeMethod(delegate)) method. Is that the way to do it? If so, then which delegate should I use for it?
Either way, I do not really think this is the way to solve the problem. The problem should be solved inside the MyClass class. The client side should not care about the fact that a thread has been used inside another class.
I hope somebody has some experience with this.
Thank you in advance once again
BramGo
I am getting an error because I am trying to access a control from an eventhandler raised by a Thread.
Longer explanation:
Hi, this is a small piece of code of a class that does some heavy calculations in a seperate thread and raises events each time new results are available.
public abstract class MyClass
{
public event EventHandler NewResults;
private int intResult;
public void DoStuff()
{
Thread th = new Thread(new ThreadStart(Calculate));
th.Priority = ThreadPriority.Lowest;
th.Start();
}
private void Calculate()
{
while(true){
//...
//calculations on intResult here
//...
OnNewResult(this);
}
}
public int Result
{
get { return intResult; }
}
protected void OnNewResults(object sender)
{
if (NewResults != null)
NewResults(sender, new EventArgs());
}
}
(My real code is a lot more complicated. It is actually an encryption algorithm I wrote and the event will be used to update a progressbar.)
Is the way I programmed the raising of the event inside the thread ok? I have the feeling I am doing something wrong here.
The exception however is not thrown in the above code though. I get it in the following part of code, where I handle the event.
/* somewhere in the constructor I put
*
* MyClass c = new MyClass();
* c.NewResults += new EventHandler(NewResults);
* c.DoStuff();
/*
void NewResults(object sender, EventArgs e)
{
MyClass c = (MyClass)sender;
txtTextBox.Text = c.Result.ToString();
}
This gives me a "cross-thread operation error because the txtTextBox was created in another thread".
I am sure that this error most be something that also other people were confronted with. What is the cleanest and usual way to solve this?
Some ideas
I read something about using the Control.Invoke(new InvokeMethod(delegate)) method. Is that the way to do it? If so, then which delegate should I use for it?
Either way, I do not really think this is the way to solve the problem. The problem should be solved inside the MyClass class. The client side should not care about the fact that a thread has been used inside another class.
I hope somebody has some experience with this.
Thank you in advance once again
BramGo