Hi All,
Just a general threading question. Would it be considered good programming
practice, or a bad idea, or whatever, to create a thread with a special name
in order to "pass parameters" to the thread.
In other words, something like this simple example:
btnRunThreads_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 10; i++)
{
ThreadStart workerStart = new ThreadStart(StartMethod);
Thread workerThread = new Thread(workerStart);
workerThread.Name = "Worker_" + i.ToString();
workerThread.Start();
}
}
void StartMethod()
{
Thread thisThread = Thread.CurrentThread;
string threadName = thisThread.Name;
string Task = threadName.Substring(threadName.IndexOf("_") + 1);
switch (Task)
{
case "1":
DoTaskOne();
break;
case "2":
DoTaskTwo();
break;
// More cases, etc.
case "10":
DoTaskTen();
break;
default:
break;
}
}
More "parameters" could be added by continuing to use a separator character
(probably an underscore, since it needs to be a valid thread name), and using
(for instance) the string.split command to enumerate them.
What say you, oh wise ones?
Thanks,
pagates