I want to make 3 threads, each of which controls a WebBroswer control. Therefore, I would like to use ThreadPool to simplify the task.
for(int i = 0;i < 3;i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(gotoWork), i));
}
WaitAll(waitHandles);
...... /
void gotoWork(object o)
{
string url = String.Empty;
int num = (int)o;
switch(num)
{
case 0:
url = "google.com";
break;
case 1:
url = "yahoo.com";
break;
case 2:
url = "bing.com";
break;
}
WebBrowser w = new WebBrower();
w.Navigate(url);
}
But I get a message that I need an STA thread that ThreadPool will never have. Before trying this method, I tried this.
Thread[] threads = Thread[3];
for(int i = 0;i < 3;i++)
{
threads[i] = new Thread(new ParameterizedStart(gotoWork);
threads[i] = SetApartmentState(ApartmentState.STA);
threads[i] = Start();
}
for(int i = 0; i < 3;i++)
{
threads[i].Join();
}
And WebBrowsers are all initialized, and everything looks fine, but only two more will actually do anything and will never be permanent. Threading was such a nightmare. Can anyone suggest a good alternative?
source
share