c# - Multithreading doesn't refresh the form -


i'm having problem multithreading, working forms. problem is: have form , 1 more class. i'm having problem refresh form screen. form screen has 5 checkboxes checked or not according 5 properties on sample class.

public boolean ip1 { get; set; }     public boolean ip2 { get; set; }     public boolean ip3 { get; set; }     public boolean ip4 { get; set; }     public boolean ip5 { get; set; }  

the main form class has function private void test() called when form loading:

new thread(test).start();  

the private void test() function 1 call sample.getcon() inside sample class , function getcon() calls more 5 threads makes pings in differently ip's , set properties of ip1, ip2, ip3... form class inside function private void test() refresh form with:

this.begininvoke((action)(() =>              checkbox1.checked = sample.ip1;             checkbox1.checked = sample.ip2;             checkbox1.checked = sample.ip3;             checkbox1.checked = sample.ip4;             checkbox1.checked = sample.ip5;     ) 

what happens form check 2 or 3 checkbox , 4 or 3, , when i'm in debug mode verify if properties true(i realize properties true) , checkbox gets checked, think it's because threads has time process. so, knows happening threads?

so saying sequence this:

  1. form starts
  2. form kicks off thread t1
  3. t1 kicks off 5 more threads (t5, t6, t7, t8, t9)
  4. t1 thread sets values of sample.ip1, 2, 3, 4, 5
  5. t1 begininvoke set checkbox.

the problem this: t1 goes step 3 step 4 while t5, 6, 7, 8, 9 still running

you should use thread.join ensure t1 doesn't continue before other background threads continue.

nb: consider using task parallel library instead of directly playing threads. http://msdn.microsoft.com/en-us/library/vstudio/dd537609.aspx


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -