Thread problem

Aug 10, 2011 at 11:15am
hi guys i want create thread in "Windows Application .Net clr" but i have one problem !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

thrd = gcnew System::Threading::Thread( gcnew System::Threading::ThreadStart(this, &Form1::onRecive) );

......


private: System::Void onRecive()
			 {
				 IPEndPoint ^client;
				 String^ str = gcnew String( "" );
				 array<System::Byte>^ buffer;

				 while ( true )
				 {
					 System::Threading::Thread::Sleep( 10 );

					 buffer = udpServer->Receive( client );

					 str = (gcnew System::Text::ASCIIEncoding)->GetString( buffer );

					 txtSenderMsg->Text += str; // break at runtime :(

// if i write this->Visible = true; then break at runtime 
				 }
			 }


i dont know but i think i cant chagne value of Control in function of thread :( how can i do this ?
i need this for socket programming in clr window application and you know i cant put my code in while ( true ) without thread because if i do it my program be lock :(
Last edited on Aug 10, 2011 at 11:19am
Aug 10, 2011 at 11:16am
tnx all of you for your guidence :D
Aug 10, 2011 at 11:42am
ummm i put this change to try-catch block and throw exc and this msg is :

"Cross-thrad operation not valid: Control 'txtSenderMsg' accessed from a thread other that the thread it was created on."

i think it must be Critical Section ! but how can i do this . i dont know
Aug 10, 2011 at 3:16pm
i found one way is InvokeRequest and Invoke but its not very interesting :( every body know a better way ?
Aug 10, 2011 at 7:09pm
There's no other way. Windows programming demands that you only control a user interface element from the thread that created it. You must use BeginInvoke() so your worker thread doesn't have to wait for the UI thread to process the request in order to get back to work.
Aug 11, 2011 at 7:46am
its too bad i really dont like it :( ok tnx all
Topic archived. No new replies allowed.