Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made an array of threads, I used delegate to subscribe the two thread function I have error in the return type


What I have tried:

C#
public Thread[] thrd = new Thread[2];
delegate System.Threading.ThreadStart SampleDelegate(int j);
public MyClass(string[] Xname,int[] XFrom, int[] XTo)
{
    int i,j;
    SampleDelegate[] vv = {runThread_0, runThread_1};
    for (i = 0; i < 2; i++)
    {
        From[i] = XFrom[i];
        To[i] = XTo[i];
        thrd[i].Name = Xname[i];
        j = i;
        thrd[i]    = new Thread(new ThreadStart(vv[i](j)));
    }
}
SampleDelegate runThread_0 = delegate (int j)  { MessageBox.Show("j=", j.ToString()); };
SampleDelegate runThread_1 = delegate (int j)  { MessageBox.Show("j=", j.ToString()); };
Posted
Updated 21-May-23 22:16pm
v9

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:
C#
delegate  SampleDelegate(int i);//<=========

Try adding a return type, for example:
C#
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[^]:
C#
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:
C#
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:
C#
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));
    }
}
 
Share this answer
 
v6
Comments
Engineer khalid 20-May-23 9:11am    
parallel - and - asynchronous - programming -in-csharp is interresting i have seen several computer connected together
working as a supper computer however i have to finished my program. working with thread is new subject for me
and it will be good advise to select Asynchronous thread among others
this is what i have done to my code
declaration changed to
delegate System.Threading.ThreadStart SampleDelegate(int i);
and allocation for thread becomes
thrd[i] = new Thread(new ThreadStart(vv[i](j)));//<==no error
i have error in
SampleDelegate runThread_0 = delegate (int jj) { MessageBox.Show("jj=", jj.ToString()); };
SampleDelegate runThread_1 = delegate (int jj) { MessageBox.Show("jj=", jj.ToString()); };
studio error is
Not all code paths return a value in anonymous method of type 'Form1.MyClass.SampleDelegate'
i still need help
Graeme_Grant 20-May-23 9:31am    
Not all code paths return a value

Self explanatory. Read this: Not All Code Paths Return a Value: How To Fix This Issue[^]
Engineer khalid 20-May-23 9:59am    
to me the equal signe in declaration of thread function is a mystery
Thread function must return SampleDelegate i have no idea how to return a SampleDelegate which it is
System. Threading. ThreadStart
For example If int i can write return(0);
i have spent many hours today and yestrday searching in the net but failed .please if you know the answer and i am sure you know ,kindly write it to me
accept my thanks to from overseas
Graeme_Grant 20-May-23 18:48pm    
Fix your sample code and I will have another look. I have used all types of multi-threading, and, IMHO, you are better off using what I have recommended.
Engineer khalid 20-May-23 22:29pm    
I fixed the sample code
Quote:
C#
delegate System.Threading.ThreadStart SampleDelegate(int j);
That defines a delegate for a function which accepts an integer and returns a ThreadStart delegate instance.
Quote:
C#
SampleDelegate runThread_0 = delegate (int j)  { MessageBox.Show("j=", j.ToString()); };
You then attempt to create an instance of your custom delegate pointing to an anonymous method which doesn't return anything.

You need to read up on delegates to understand what they are and how they're used:
Delegates - C# Programming Guide | Microsoft Learn[^]

If you want to use your custom delegate, you would need to change the anonymous methods to return a delegate instance:
C#
SampleDelegate runThread_0 = delegate (int j) 
{
    return new ThreadStart(delegate() 
    {
        MessageBox.Show("j = " + j);
    });
};
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900