02. december 2010 - 09:39
Der er
7 kommentarer og 1 løsning
hjælp til delegate (opdatere textbox fra en anden thread) c++/cli
har prøvet lidt forskelligt for at få det til at virke men render ind i samme problem hver gang... her er koden: private: delegate void AppendTextDelegate(String^ text); private: void AppendText(String^ text) { if(textBox4->InvokeRequired) { textBox4->Invoke(gcnew AppendTextDelegate (&clientservertest::Form1::AppendText), text ); } else { textBox4->Text = text; } } og i tråden skriver jeg så bare: AppendText(received); fejlen jeg får er: error C3352: 'void clientservertest::Form1::AppendText(System::String ^)' : the specified function does not match the delegate type 'void (System::String ^)' nogen der kan hjælpe mig?
Annonceindlæg fra Infor
02. december 2010 - 11:06
#2
hvorfor skulle det være et problem? det er nu heller ikke den linje den melder fejl på, men denne: textBox4->Invoke(gcnew AppendTextDelegate (&clientservertest::Form1::AppendText), text );
02. december 2010 - 11:31
#3
Kommer de private: ikke fra klasse erklæringen? hvis det er tilfældet og du ikke har nogle friends erklæringer eller de er i samme klasse alle sammen så kan jeg ikke se det kan fungerer. Måske mangler der bare noget yderligere kode så jeg kan se hvad du gør?
02. december 2010 - 12:50
#4
det ligger alt sammen inden i form1.h og under samme class... jeg har oprettet alle function osv inde i form1.h..... ved godt det nok ikke er det smarteste at gøre men gad ikke lige dele det op her kommer hele koden efter #pagma endregion: private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) { this->radioButton3->Enabled = false; this->radioButton4->Enabled = false; this->textBox4->Enabled = false; this->button5->Enabled = false; this->button4->Enabled = false; this->button3->Enabled = true; this->toolStripStatusLabel2->Text = "server stopped"; } private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { if(this->textBox5->Text == "") { System::Windows::Forms::MessageBox::Show("mangler port"); } else { this->button3->Enabled = false; this->radioButton3->Enabled = true; this->radioButton4->Enabled = true; this->textBox4->Enabled = true; this->button5->Enabled = true; this->button4->Enabled = true; tcpserver = gcnew TcpListener(IPAddress::Any,Convert::ToInt32(this->textBox5->Text)); listenthread = gcnew Thread(gcnew ThreadStart(this,&clientservertest::Form1::listenforclients)); listenthread->Start(); this->toolStripStatusLabel2->Text = "server started"; } } private: System::Void radioButton4_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { this->textBox4->Enabled = false; this->button5->Enabled = false; } private: System::Void radioButton3_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { this->textBox4->Enabled = true; this->button5->Enabled = true; } private: System::Void radioButton2_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { this->textBox3->Enabled = false; this->button2->Enabled = false; array<Byte>^ message = gcnew array<Byte>(4096); int bytesread; while(true) { bytesread = 0; try { bytesread = clientstream->Read(message,0,4096); } catch(...) { //Socket error break; } if(bytesread==0) { //Client disconnected break; } ASCIIEncoding^ encoder = gcnew ASCIIEncoding(); String^ received = encoder->GetString(message,0,bytesread); this->textBox3->Text = received; } } private: System::Void radioButton1_CheckedChanged(System::Object^ sender, System::EventArgs^ e) { this->textBox3->Enabled = true; this->button2->Enabled = true; } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if(this->textBox1->Text == "" || this->textBox2->Text == "") { System::Windows::Forms::MessageBox::Show("du mangler at skrive ip eller port"); } else { //TcpClient^ tcpclient = gcnew TcpClient(); //NetworkStream^ clientstream; tcpclient->Connect(this->textBox1->Text, Convert::ToInt32(this->textBox2->Text)); clientstream = tcpclient->GetStream(); this->toolStripStatusLabel1->Text = "Connected"; } } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { ASCIIEncoding^ encoder = gcnew ASCIIEncoding(); array<Byte>^ buffer = encoder->GetBytes(this->textBox3->Text); clientstream->Write(buffer, 0, buffer->Length); clientstream->Flush(); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { this->radioButton1->Select(); this->radioButton3->Select(); } private: void listenforclients(void) { tcpserver->Start(); while(true) { TcpClient^ client = tcpserver->AcceptTcpClient(); Thread^ clientthread = gcnew Thread(gcnew ParameterizedThreadStart(this,&clientservertest::Form1::handleclientcomm)); clientthread->Start(client); } } private: void handleclientcomm(Object^ client) { TcpClient^ tcpclient = (TcpClient^)client; NetworkStream^ clientstream = tcpclient->GetStream(); array<Byte>^ message = gcnew array<Byte>(4096); int bytesread; while(true) { bytesread = 0; try { bytesread = clientstream->Read(message,0,4096); } catch(...) { //Socket error break; } if(bytesread==0) { //Client disconnected break; } ASCIIEncoding^ encoder = gcnew ASCIIEncoding(); String^ received = encoder->GetString(message,0,bytesread); //Console::WriteLine(received); //this->textBox4->Invoke(gcnew Form1::test_delegate(&Form1::update_text), received); AppendText(received); } tcpclient->Close(); } private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) { ASCIIEncoding^ encoder = gcnew ASCIIEncoding(); array<Byte>^ buffer = encoder->GetBytes(this->textBox4->Text); clientstream->Write(buffer, 0, buffer->Length); clientstream->Flush(); } //public: delegate void OutputUpdateDelegate(String^ data); // //public: void UpdateTextBox(String^ data) // { // if ( textBox4->InvokeRequired ) // textBox4->Invoke(gcnew OutputUpdateDelegate(&Form1::OutputUpdateCallback), data); // else // OutputUpdateCallback(data); //call directly // } // // private: static void OutputUpdateCallback(String^ data) // { // textBox4->Text = data; // } //public: delegate void test_delegate(String^ s); // static void update_text(String^ s) // { // textBox4->Text = s; // } public: delegate void AppendTextDelegate(String^ text); public: void AppendText(String^ text) { if(textBox4->InvokeRequired) { textBox4->Invoke(gcnew AppendTextDelegate(&clientservertest::Form1::AppendText), text ); } else { textBox4->Text = text; } }
02. december 2010 - 15:31
#5
textBox4->Invoke(gcnew AppendTextDelegate (&clientservertest::Form1::AppendText), text ); skal det være textBox4->Invoke(gcnew AppendTextDelegate (&clientservertest::Form1::AppendText, text )); ?