I downloaded the MS Marine Biology Simulation for J#. The Conversion to J#
2005 went w/o errors. But when I run it, I get:
Cross-thread operation not valid: Control 'pond21' accessed from
a thread other than the thread it was created on.
What gives?
Hi Tom,
Thanks for reporting this issue.
We are aware of this and are on the way to upload the updated MBS Case
Study for VS 2005.
To unblock you, I will first describe what the issue is and how to fix it.
Issue:
The case study had a bug in the sense that; in it, an object called
'pond21'(which is a Winforms Control) is accessed from different threads.
"Access to Windows Forms controls is not inherently thread safe. If you
have two or more threads manipulating the state of a control, it is
possible to force the control into an inconsistent state".
From .Net 2.0 (VS 2005 i.e.) onwards the debugger is going to inform the
developer that a control is accessed from a thread different from the one
that created it - the debugger will be raising an InvalidOperationException
in such a case.
Fix:
'Refresh()' - This method in the Winforms Control 'Pond' is accessed from a
thread different from the one that created the control. What we are
essentially doing is synchronizing all calls to the method 'Refresh()'.
Change "public void showEnv()" in Pond.jsl to <<
public void showEnv() {
/** This method is called from more than one threads.
Following if block is there to avoid unsynchronized access **/
if (this.get_InvokeRequired()) {
this.BeginInvoke(new MethodInvoker(CallRefresh));
}
else
this.Refresh();
}
public void CallRefresh() {
this.Refresh();
}
Thanks,
Diganta Roy
Microsoft J# .NET Product Team.
--------------------
>Thread-Topic: Marine Biology Simulation w/J# 2005 Express
>thread-index: AcXyGP90rWTXHqHQRvuxiHlIswmO9g==
[quoted text clipped - 26 lines]
>
>What gives?