G
Guest
I have a Thread making a callback to a Form function that alters Form data.
Of course, the debugger is telling me, "Cross-thread operation not valid:
Control 'FormTest' accessed from a thread other than the thread it was
created on."
But I am locking before I touch the shared data, so do I actually need to
worry about this, or is it just the debugger being oversimplistic?
Specifically, I have:
public delegate void TestEvent(ref string Payload);
public class FormTest : System.Windows.Forms.Form
{
...
void Run
{
TestHarness test = new TestHarness();
test.Callback += TestFunction; // Callback is of type
TestEvent
Thread t = new Thread(new ThreadStart(test.Go));
t.IsBackground = true;
t.Start();
}
private void TestFunction(ref string Payload)
{
lock(this) WriteOut(ref Payload); // ??? Doesn't this lock
prevent thread synchronization problems???
}
private void WriteOut(ref string Payload)
{
this.Text = Payload;
}
Of course, the debugger is telling me, "Cross-thread operation not valid:
Control 'FormTest' accessed from a thread other than the thread it was
created on."
But I am locking before I touch the shared data, so do I actually need to
worry about this, or is it just the debugger being oversimplistic?
Specifically, I have:
public delegate void TestEvent(ref string Payload);
public class FormTest : System.Windows.Forms.Form
{
...
void Run
{
TestHarness test = new TestHarness();
test.Callback += TestFunction; // Callback is of type
TestEvent
Thread t = new Thread(new ThreadStart(test.Go));
t.IsBackground = true;
t.Start();
}
private void TestFunction(ref string Payload)
{
lock(this) WriteOut(ref Payload); // ??? Doesn't this lock
prevent thread synchronization problems???
}
private void WriteOut(ref string Payload)
{
this.Text = Payload;
}