A
Andrew Bullock
Hi, I've got a wierd problem...
Here is my code:
public partial class frmMain : Form
{
private MYClass myClass;
private void btnRun_Click(object sender, EventArgs e)
{
myClass = new MYClass(new CallBack(doStuff));
Thread t1 = new Thread(new ThreadStart(myClass.Run));
t1.Start();
}
private void doStuff()
{
pictureBox1.Image = myClass.getPicture();
}
}
///////////////////////////////////////
public delegate void CallBack();
class MYClass
{
private CallBack CallBackMethod;
public MYClass(CallBack cb)
{
CallBackMethod = cb;
}
public void Run()
{
// do something useful
CallBackMethod();
}
public Bitmap getPicture()
{
// return an img
}
}
///////////////////////////////////////////////////////////////////
in doStuff() i grab data back from myClass like this:
myClass.getData();
and then display it in pictureboxes, listboxes etc in the normal way
(not using invoke - because i thought the point in callbacks was to
avoid invoke). SOMETIMES (ive not worked out when/why) I get a
cross-threading error:
"Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on."
When the debugger takes me to break mode to display the error, if i just
click the run button again, it continues fine :s Sometimes this error
does not occur at all.
Can anyone tell me why, and then what I can do about it?
Thanks
Andrew
Here is my code:
public partial class frmMain : Form
{
private MYClass myClass;
private void btnRun_Click(object sender, EventArgs e)
{
myClass = new MYClass(new CallBack(doStuff));
Thread t1 = new Thread(new ThreadStart(myClass.Run));
t1.Start();
}
private void doStuff()
{
pictureBox1.Image = myClass.getPicture();
}
}
///////////////////////////////////////
public delegate void CallBack();
class MYClass
{
private CallBack CallBackMethod;
public MYClass(CallBack cb)
{
CallBackMethod = cb;
}
public void Run()
{
// do something useful
CallBackMethod();
}
public Bitmap getPicture()
{
// return an img
}
}
///////////////////////////////////////////////////////////////////
in doStuff() i grab data back from myClass like this:
myClass.getData();
and then display it in pictureboxes, listboxes etc in the normal way
(not using invoke - because i thought the point in callbacks was to
avoid invoke). SOMETIMES (ive not worked out when/why) I get a
cross-threading error:
"Cross-thread operation not valid: Control '' accessed from a thread
other than the thread it was created on."
When the debugger takes me to break mode to display the error, if i just
click the run button again, it continues fine :s Sometimes this error
does not occur at all.
Can anyone tell me why, and then what I can do about it?
Thanks
Andrew