Link to home
Create AccountLog in
Avatar of John500
John500Flag for United States of America

asked on

Unhandled exception - cross thread operation not valid (upgraded project from VS .Net 2002)

Greetings:

I recently updated a Windows Form Application from .Net 2002 to 2005.  In the previous version I had no problems calling a ListBox but now I get the following:

Cross-thread operation not valid: Control 'listBox21' accessed from a thread other than the thread it was created on.

Here's the function I'm declaring in Form.h:

static void ListBox21Msg(string strValue);

The code for this function is posted below and the line which the error occurs on is:

String *ListBox21Str = __gc new String(strValue.c_str());

Thanks!







void Form1::ListBox21Msg(string strValue)
{
	char		datebuf[9];			// stores the date obtained from _strdate()
	char		timebuf[9];			// stores the time obtained from _strtime()		
	char		LogBuffer[1024];		// holds the message which is converted into a string
 
 
	string		LogMessage1, LogMessage2;
    	_strdate(datebuf);				// get current date
	_strtime(timebuf);				// get current time
	sprintf(LogBuffer, "**********   ListBox5Msg()   \t********** \t  %s %s   -   ", datebuf, timebuf);
 
	LogMessage1 = LogBuffer;
	LogMessage2 = strValue;
	LogMessage1 = LogMessage1 + LogMessage2;
 
	//WriteToFile("C:\\ListBox21Msg.txt", LogMessage1 + "\n");
 
	if(Form1::listBox21->Items->Count > 20)
		Form1::listBox21->Items->Clear();
 
	Form1::listBox21->BeginUpdate();
	String *ListBox21Str  = __gc new String(strValue.c_str());
	Form1::listBox21->Items->Add(ListBox21Str);
	Form1::listBox21->EndUpdate();
	return;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of John500

ASKER

jkr,

thanks, I don't know how I keep missing the managed C++ zone.  I did look for it this time and assumed it fell under .Net ...  When I look to the top of this question I currently see the following:

Home Programming Languages .NET Unhandled exception - cross thread operation not valid (upgraded p...

How are things change?

Thanks.

Avatar of John500

ASKER

ZachSmith,

Thanks for the input.  I'm uncertain as to how the code provided is arranged.  Here's what I'm not getting:

1)  Since the error is occuring on this line:
String *ListBox21Str = __gc new String(strValue.c_str());

yet the error is calling out  'listBox21'  which doesn't occur on that line - what then are the parameters for this function:
SetControlPropertyValue(         )

2)  I'm not sure where the function below should be declared.  I'm sure it has to be in Form1.h, but where?

delegate void SetControlValueCallback(Control oControl, string propName, object propValue);

3)  The function definition below includes the word 'private' which doesn't go in Form1.cpp.  Hence, the word 'private' is throwing me off for the definition and the word delegate is throwing me off for the declaration.

Thanks!

private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
	if (oControl.InvokeRequired){
		SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
		oControl.Invoke(d, new object[] { oControl, propName, propValue });
	}
	else{
		Type t = oControl.GetType();
		PropertyInfo[] props = t.GetProperties();
		foreach (PropertyInfo p in props)
	{
	if (p.Name.ToUpper() == propName.ToUpper()){
		p.SetValue(oControl, propValue, null);
	}
}
}  //  I GUESS THIS BRACKET IS A TYPO
}  //  I GUESS THIS BRACKET IS A TYPO

Open in new window

Avatar of John500

ASKER

One thing I found which worked in the Form's constructor is this:

this->CheckForIllegalCrossThreadCalls = false;

I'm not saying this is the best approach but all the more reason I could use help on the approach you suggested.

Thanks

public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			this->CheckForIllegalCrossThreadCalls = false;
 
			InitializeComponent();

Open in new window

Avatar of John500

ASKER

ZachSmith,

Can you take a look at my post  -  http:#21070422


Right now I'm getting by with the line below but I'd rather do things right if I could:

this->CheckForIllegalCrossThreadCalls = false;

Thanks
Avatar of John500

ASKER

ZachSmith,  Thanks - I see the light now on this solution.  Reminds me of John 12:35