FIX: Threading runtime in 2.0 framework

Questions and Answers

Threading runtime in 2.0 framework


DrDandle 08-12-2006, 8:30 AM

I converted the souce to 2.0 and when I press the execute button I get the following error:

System.InvalidOperationException was unhandled
  Message="Cross-thread operation not valid: Control 'tLog' accessed from a thread other than the thread it was created on."
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.TextBoxBase.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.TextBoxBase.set_Text(String value)
       at System.Windows.Forms.TextBox.set_Text(String value)
       at _4XLab.NET.Framework.ClearLog() in C:\Documents and Settings\randyd\My Documents\Visual Studio 2005\4xlab\4XLab[1].NET_5_2415\4XBase.DLL\Framework.cs:line 231
       at _4XLab.NET.SystemTester.Execute() in C:\Documents and Settings\randyd\My Documents\Visual Studio 2005\4xlab\4XLab[1].NET_5_2415\4XBase.DLL\SystemTester.cs:line 54
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

 

 

I looked in the related help topic labeled:

How to: Make Thread-Safe Calls to Windows Forms Controls 

I found a couple interesting things:

 

This exception occurs reliably during debugging and, under some circumstances, at run time. You are strongly advised to fix this problem when you see it. You might see this exception when you debug applications that you wrote with the .NET Framework prior to .NET Framework version 2.0.

NoteNote

You can disable this exception by setting the value of the CheckForIllegalCrossThreadCalls property to false. This causes your control to run the same way as it would run under Visual Studio 2003.

 

I added the following line of code to LabGUI() and the problem went away however I think this is only a temporary fix.  I am not sure what kind of strange behavior might occur from this threading issue.

 

Control.CheckForIllegalCrossThreadCalls = false;

 

Thanks

Randy

 

Re: Threading runtime in 2.0 framework


asimon 08-12-2006, 8:56 AM

Good research, if you see issues like this, this is the forum for that.

The "temporary fix" might be appropriate for this application. The other solution might be to add a public method to the GUI class that uses a lock to modify the textbox control. Have Framework call this method via an instance sent at initialization time.

A new version that solved some other issues and adds new features has been posted.

FIX: Threading runtime in 2.0 framework


asimon 09-04-2006, 12:35 PM
This design pattern seems to be the proper solution to fix the threading issue with the .NET Framework 2.0

delegate void SetTextCallback(string text);
private void Log(string pText)
{
  // InvokeRequired required compares the thread ID of the
  // calling thread to the thread ID of the creating thread.
  // If these threads are different, it returns true.
  if (this.textBox1.InvokeRequired)
  {
    SetTextCallback d = new SetTextCallback(Log);
    this.Invoke(d, new object[] { pText });
  }
  else
  {
    textBox1.AppendText(pText);
    textBox1.AppendText(
"\r\n");
  }
}

Re: Threading runtime in 2.0 framework


avinash66 10-06-2006, 2:09 PM
from where I can download 4xbase.dll sourcecode \framework.cs
Forex Lab .NET | 4X Lab .NET © Copyright 2005-2006 ASCSE LLC | Disclaimer

Powered by Community Server, by Telligent Systems