how to use thread in for loop in c#
-
Hi i did not understand do you want the code to be changed to for (int index = startIP; index <= EndIP; index++) { ipVal = startSubnet + "." + index.ToString(); Thread wmithread = new Thread(() => PerformWMIOperation(ipVal.ToString().Trim(), txtUserName.Text.ToString().Trim(), txtPassword.Text.ToString(), txtDomain.Text.ToString(), dtAssetValues, chkImpersonate.Checked, recordnumber)); wmithread.Start(); wmithread.Join(); }
That wouldn't help. The problem is this:
superselector wrote:
wmithread.Start(); wmithread.Join();
That's like calling that ping operation without a thread, except this way resources are wasted as well. Or to put it differently, it's like hiring a team to play a game of monopoly, in order to do it faster. That doesn't work. If everyone takes turns anyway, you might as well do it by yourself. You need an array of threads. Fill the array, start them all, then, and only then, join them all. Besides, what's with the WMI stuff? Do you need something that the Ping class[^] can't do?
-
That wouldn't help. The problem is this:
superselector wrote:
wmithread.Start(); wmithread.Join();
That's like calling that ping operation without a thread, except this way resources are wasted as well. Or to put it differently, it's like hiring a team to play a game of monopoly, in order to do it faster. That doesn't work. If everyone takes turns anyway, you might as well do it by yourself. You need an array of threads. Fill the array, start them all, then, and only then, join them all. Besides, what's with the WMI stuff? Do you need something that the Ping class[^] can't do?
can you please give a sample code to implement this
-
can you please give a sample code to implement this
-
Ok.. it's not that hard though.
Thread[] threads = new Thread[something];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(something);
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
threads[i].Join();hi with this approach , I am missing data .. means i am not getting output for all the IPs
-
Ok.. it's not that hard though.
Thread[] threads = new Thread[something];
for (int i = 0; i < threads.Length; i++)
{
threads[i] = new Thread(something);
threads[i].Start();
}
for (int i = 0; i < threads.Length; i++)
threads[i].Join();The ip address is getting overlapped. Means two rows are having same ip address.
-
The ip address is getting overlapped. Means two rows are having same ip address.