Welcome to 4XLab.NET : Forex Lab Community Server Sign in | Join | Faq

Questions and Answers

Started by DrDandle at 08-12-2006 8:30 AM. Topic has 3 replies.

Print Search
Sort Posts:    
   08-12-2006, 8:30 AM
DrDandle is not online. Last active: 8/19/2008 1:36:14 AM DrDandle

Top 10 Posts
Joined on 08-05-2006
Oregon
Posts 4
Threading runtime in 2.0 framework

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

 


   Report 
   08-12-2006, 8:56 AM
asimon is not online. Last active: 1/23/2010 8:15:16 PM asimon

Top 10 Posts
Joined on 03-07-2006
Posts 140
Re: Threading runtime in 2.0 framework

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.


   Report 
   09-04-2006, 12:35 PM
asimon is not online. Last active: 1/23/2010 8:15:16 PM asimon

Top 10 Posts
Joined on 03-07-2006
Posts 140
FIX: Threading runtime in 2.0 framework
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");
  }
}


   Report 
   10-06-2006, 2:09 PM
avinash66 is not online. Last active: 10/7/2006 8:24:57 AM avinash66

Top 10 Posts
Joined on 10-06-2006
Posts 3
Re: Threading runtime in 2.0 framework
from where I can download 4xbase.dll sourcecode \framework.cs
   Report 
4XLab.NET : For... » 4XLab.NET » Questions and A... » FIX: Threading runtime in 2.0 framework

Powered by Community Server, by Telligent Systems