Reputation: 652
I'm having a problem with multithreading, working with forms. The problem is: I have a form and one more class. I'm having problem to refresh the form screen. The form screen has five checkboxes that are checked or not according to my five properties on the 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 a function private void test()
which is called when the form is loading:
new Thread(test).Start();
the private void test()
function does one call to the sample.getCon()
inside the sample
class and this function getCon()
calls more five Threads that makes pings in differently IP's and then set the properties of ip1, ip2, ip3...
the form class inside the function private void test()
refresh the 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 is that the form sometimes check a 2 or three checkbox and sometimes 4 or 3, and when I'm in the debug mode to verify if all the properties are true(I realize that all properties are true) and that all checkbox gets checked, I think it's because that the Threads has time to process. So, someone knows what is happening with the Threads?
Upvotes: 0
Views: 74
Reputation: 10600
So you are saying the sequence is this:
The problem is this: T1 goes from step 3 to step 4 while T5, 6, 7, 8, 9 are still running
You should use Thread.Join to ensure that T1 doesn't continue before those other background threads continue.
NB: consider using the Task Parallel Library instead of directly playing with threads. http://msdn.microsoft.com/en-us/library/vstudio/dd537609.aspx
Upvotes: 2