multithreading - C# , how reach something in current thread that created in other thread? -


i'm writing chat client/server app c# , have problem threading. wrote simple code showing problem.

i used thread_1 showing form show second (maybe thread_1 terminated , closed form , isalive said alive !). thread_2 try reach texbox created on main thread shows me:

"cross-thread operation not valid: control 'textbox2' accessed thread other thread created on."

i dont know how solve first problem solved second problem backgroundworker thread. there way?

public partial class form1 : form {     public form1()     {         initializecomponent();     }      thread t1;     thread t2;       private void button1_click(object sender, eventargs e)     {          t1 = new thread(dothread1);         t1.name = "thread_1";          t2 = new thread(dothread2);         t2.name = "thread_2";          t1.start();         t2.start();          messagebox.show(t1.isalive.tostring());     }      private void dothread1()     {         form frm2 = new form();         frm2.show();     }       private void dothread2()     {         try         {             (int j = 10000; j > 0; j--)                 textbox.text = j.tostring();         }         catch (exception ex)         {             messagebox.show(ex.message);         }     }   } 

as linkerro mentioned, want take out thread call thread 1 ui in thread when start (all programs have single main thread start on). on right track though, want put long-running tasks on separate thread doesn't block ui. trick cannot directly maniuplate ui objects background threads, must manipulated thread owns them (which error message getting saying).

luckily there easy way accomplish in .net. in wpf use uicomponent.dispatcher.invoke() , winforms use uicomponent.invoke(). allows background thread step on thread ui component lives update it.

invoke takes delegate represents action run on ui thread. in example pass in action initialized using lambada expression, taking no parameters , returning no value.

try this

private void dothread2() {     try     {         (int j = 10000; j > 0; j--)         {             textbox.invoke(new action(() =>                 textbox.text = j.tostring()));         }     }     catch (exception ex)     {         messagebox.show(ex.message);     } } 

here full example of how can tasks. see while counting can freely move window around , not lock up. take out task , leave loop , see how window freezes since loop block ui thread.

public partial class form1 : form {     public form1()     {         initializecomponent();     }      private void button1_click(object sender, eventargs e)     {         task.factory.startnew(() =>         {             (int x = 0; x < 100000000; x++)             {                 label1.invoke(new action(() =>                     label1.text = x.tostring()));             }         });     } } 

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -