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.

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.

I have no idea what invoke does. Never heard of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.