0

All my communication using this delegate and function are great between forms, but this thread for receiving compiles and all, but then throws this error. I have posted the important sections of my code. If you think I need more, let me know. Thanks.

I am getting a Cross thread operation not valid: Control "Label1" accessed from a thread other than the thread it was created error.

In my rs232 thread I have

unsigned char messageBuf[512];
Rs232com->Open();

Rs232com->Parity = Parity::None;
Rs232com->DataBits = 8;
Rs232com->StopBits = StopBits::One;

int index = 0;

do
{
	messageBuf[index] = Rs232com->ReadChar();
	index ++;
}
while(messageBuf[index - 1] != (char)0x3F);

messageBuf[index - 1] = (char)0x0;

Rs232com->Close();

if(messageBuf[0] == (char)0x01)
{
                DataArch->setlabel1("in");
}

String^ update = "GO";
NameChangeCallbackFn(update);

DataArch is an instance of my dataclass that I pass around the forms as well as this thread.

Then in my main form

public delegate System::Void SendStringDelegate(System::String^ name);

MainForm(DataSource^ DataArch)
{
           DataClass = DataArch;

           rs232->NameChangeCallbackFn = gcnew SendStringDelegate(this, &WCS::MainForm::NameChangeCallbackFn);

private: System::Void NameChangeCallbackFn(System::String^ name) 
{
           //if (this->InvokeRequired)
           //{
	           // Invoke(gcnew SendStringDelegate(this, &WCS::MainForm::NameChangeCallbackFn));

            this->label1 = DataClass->getlabel1();    //Line of the error
            //}
}

I know I have to use invoke, but I dont know how to implement it. Any help would be appreciated. Thanks.

2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

Could someone at least explain invoke and how it is used? or link me to something that may make sense. Ive been googling and cant find anything that I understand. Thanks either way.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.