Vivek,
Did you ever get any kind of answer to this? I'm trying to dynamically create a thread array in c# as well. Basically I am calling some data from a table. Each row in this table has some values I need. For each row, I'll need a thread spawned which calls a method that handles that data. (Each thread communicates with a 3rd party web service, so that's why I'm threading the method call)
something like so:
private int _methodCount;
public int MethodCount
{
get
{
return SafeConvert.ToInt32(_methodCount);
}
set
{
_methodCount = value;
}
}
private Thread [] arrThread
{
get
{
return new Thread[MethodCount];
}
}
private Thread providerThread
{
get
{
return new Thread(new ThreadStart(GetResults)); // GetResults is the method that interacts with 3rd party web services
}
}
void Method()
{
ds = a dataset
MethodCount = ds.Tables[0].Rows.Count;
int x = 0;
// Loop through the row count and spawn a thread for each
foreach (DataRow dr in ds.Tables[0].Rows)
{
// ... do some code here, then start the thread for this row's data
providerThread.Start();
arrThread[x] = providerThread;
x++;
}
// Now make sure all therads are completed, if not, don't continue with final code
for (int i = 0; i < MethodCount; i++)
{
if (arrThread[i].ThreadState == ThreadState.Running)
Thread.Sleep(1);
}
// ... final code here to run after all threads are completed.
}
Any thoughts???
|