Hello!
Help fix the error:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information: Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.
The error occurs when working with a stream ... I have an application client that should send and receive messages ... I accept these messages in a stream:
File.cpp namespace Client_WFA { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Runtime::InteropServices; void Form1::trdMethod(){ while(....) { Invoke(myDelegate); } //Thread::Sleep(0); } void Form1::delegation(){ richTextBox1->Text=richTextBox1->Text+"\n"+systemstring; } Form1::Form1(void){ InitializeComponent(); trdc = gcnew Thread(gcnew ThreadStart(this, &Form1::trdMethod)); trdc->Start(); } void Form1::InitializeComponent(void){ // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(311, 272); this->Controls->Add(this->richTextBox2); this->Controls->Add(this->richTextBox1); this->Controls->Add(this->label4); this->Controls->Add(this->label3); this->Controls->Add(this->Send); this->Controls->Add(this->textBox1); this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D; this->MaximizeBox = false; this->Name = L"Form1"; this->Opacity = 0.7; this->Text = L"Client"; this->FormClosing += gcnew System::Windows::Forms::FormClosingEventHandler(this, &Form1::Form1_FormClosing); this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); myDelegate = gcnew AddListItem( this, &Form1::delegation);//<============== } void Form1::Form1_Load(System::Object^ sender, System::EventArgs^ e) {}
}
File.h namespace Client_WFA { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Threading; /// <summary> public ref class Form1 : public System::Windows::Forms::Form { String ^systemstring; private: Thread^ trdc; private: System::Void trdMethod(); delegate void AddListItem(); AddListItem^ myDelegate; void delegation(); public: void socket_init(); Form1(void); protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (trdc->IsAlive)// уничтожаем поток trdc->Abort(); if (components) { delete components; } } private: System::Windows::Forms::TextBox^ textBox1; private: System::Windows::Forms::Button^ Send; public: System::Windows::Forms::Label^ label3; private: System::Windows::Forms::Label^ label4; private: System::Windows::Forms::RichTextBox^ richTextBox1; private: System::Windows::Forms::RichTextBox^ richTextBox2; private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void); #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e); }; }
I realized that the program can not change the value of the component from the stream ... How can I fix this?