I have a function like this:
private void GetRSS(int start, int end)
{
for (int i = start; i < end; i++)
{
string content = string.Empty;
using (WebClient client = new WebClient())
{
//some code here get html content
}
// some code here parse content
}
}
In order to minimize the amount of time running to get all the needed data, I would like to run the function 4 times with different ranges at the same time and then merge the results or use a thread safe list or dictionary.
My question is, how could I run this function in 4 separated threads and still be able to control if one of the threads still working or not to know when it ends ?
My first idea was to declare each thread:
private Thread _controler;
private Thread _worker1;
private Thread _worker2;
private Thread _worker3;
private Thread _worker4;
private bool _isRunning = false;
Then I would start the controler and from inside the controler I would call each thread to execute the function above and keep track of each thread from the controler, something like:
private void _ControlerStart()
{
_worker1 = new Thread(GetRSS);
try
{
_worker1.Start(1, 7711);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
// repeat the above to all the 4 threads
_isRunning = true;
while (_isRunning)
{
if (_worker1.ThreadState != ThreadState.Running && _worker2.ThreadState != ThreadState.Running && _worker3.ThreadState != ThreadState.Running && _worker4.ThreadState != ThreadState.Running)
_isRunning = false;
}
MessageBox.Show("Done");
}
While thinking on all this mess above I realize d that this is not the best way to do what I wanted and here I am ;).
How can I manage more than 1 thread to run the same function and yet be able to know when each thread has ended working to close or save or merge the data or do whatever else I have left to do from a main thread?