Your code is difficult to read, it's not the 1980s, so use variable names to make the code self describing. It's easier on you, and easier on people like us when you post questions like this one. Learn more here:
C# Coding Conventions | Microsoft Learn[
^]
Why raw Threads? why not Parallel or Asynchronous Programming? Learn more here:
Parallel and Asynchronous Programming - Dot Net Tutorials[
^]
To address the core question, why you are getting the error:
cannot convert from 'void' to 'System.Threading.ThreadStart'
You use a void Delegate:
delegate SampleDelegate(int i);
Try adding a return type, for example:
delegate int SampleDelegate(int i);
There are other issues with the sample given, however, I have given you a starting point to work the problem.
UPDATE
Here is a "working" Console app example, of what I think you are trying to do, using the
Thread Class (System.Threading) | Microsoft Learn[
^]:
internal class Program
{
private static void Main(string[] args)
{
Thread[] threadsArray = new Thread[2];
for (int i = 0; i < threadsArray.Length; i++)
{
int num = i + 1;
Thread thread = new Thread(() => DoSomething(num));
thread.Name = string.Format("My Thread {0}", num);
threadsArray[i] = thread;
threadsArray[i].Start();
}
foreach (Thread thread in threadsArray)
thread.Join();
}
private static void DoSomething(int i)
{
Console.WriteLine(string.Format("Thread: {0}, i = {1}",
Thread.CurrentThread.Name, i));
}
}
UPDATE #2
And here is a version
Using Delegates - C# Programming Guide | Microsoft Learn[
^]
for the sample above:
internal class Program
{
delegate void delegateMethod(int num);
private static void Main(string[] args)
{
Thread[] threadsArray = new Thread[2];
delegateMethod myMethod = DoSomething;
for (int i = 0; i < threadsArray.Length; i++)
{
int num = i + 1;
Thread thread = new Thread(() => myMethod(num));
thread.Name = string.Format("My Thread {0}", num);
threadsArray[i] = thread;
threadsArray[i].Start();
}
foreach (Thread thread in threadsArray)
thread.Join();
}
private static void DoSomething(int i)
{
Console.WriteLine(string.Format("Thread: {0}, i = {1}",
Thread.CurrentThread.Name, i));
}
}
Now that you have the
delegate
, you can interchange methods passed to the thread. You could have a method called
DoSomethingElse
with the same signature, then based on a condition, set the
myMethod
to it instead of
DoSomething
. For example:
internal class Program
{
delegate void delegateMethod(int num);
private static void Main(string[] args)
{
Thread[] threadsArray = new Thread[2];
for (int i = 0; i < threadsArray.Length; i++)
{
int num = i + 1;
delegateMethod myMethod;
if (num == 1)
myMethod = DoSomethingElse;
else
myMethod = DoSomething;
Thread thread = new Thread(() => myMethod(num));
thread.Name = string.Format("My Thread {0}", num);
threadsArray[i] = thread;
threadsArray[i].Start();
}
foreach (Thread thread in threadsArray)
thread.Join();
}
private static void DoSomething(int i)
{
Console.WriteLine(string.Format("Thread: {0}, i = {1}",
Thread.CurrentThread.Name, i));
}
private static void DoSomethingElse(int i)
{
Console.WriteLine(string.Format("Thread: {0}, i * 10 = {1}",
Thread.CurrentThread.Name, i * 10));
}
}