-1

Im trying to call a function "disp" and parse through the number 1, having issues with it stating that the method name is expected. If I could understand how to parse a number and the function in the threadstart that would be amazing. Thank you in advance

error picture

    class Class1
    {
        public static void disp(int num)
        {

                try
                {
                    Console.WriteLine(num);
                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR");
                }

            Console.WriteLine("Done");


        }

        public static void Main(string[] args)
        {
            ThreadStart ts1 = new ThreadStart(disp(1));
            Thread t = new Thread(ts1);
            t.Start();
            Console.ReadLine();

        }
    }
}
  • 1
    Does this answer your question? [Thread vs Threadstart](https://stackoverflow.com/questions/29862234/thread-vs-threadstart) and [C# Threads -ThreadStart Delegate](https://stackoverflow.com/questions/1782210/c-sharp-threads-threadstart-delegate) and [How to create a thread?](https://stackoverflow.com/questions/811224/how-to-create-a-thread) –  Jul 27 '21 at 04:31
  • When you wrote "parse" did you mean "pass"? It's one of those typos that changes the whole meaning of what you're saying. Parse is e.g. convert a string to a number, Pass is e.g. send an argument value to a method – Caius Jard Jul 27 '21 at 05:11

2 Answers2

-1

According to ThreadStart Delegate:
To start a thread using a static thread procedure, use the class name and method name when you create the ThreadStart delegate. Beginning in version 2.0 of the .NET Framework, it is not necessary to create a delegate explicitly. Specify the name of the method in the Thread constructor, and the compiler selects the correct delegate.

using System;
using System.Threading;
                    
public class Program
{
    public static void disp(int num)
    {

        try
        {
            Console.WriteLine(num);
            Thread.Sleep(500);
        }
        catch (Exception e)
        {
            Console.WriteLine("ERROR");
        }

        Console.WriteLine("Done");
    }
    
    public static void disp1()
    {
        disp(1);
    }
    
    public static void Main()
    {
        ThreadStart ts1 = new ThreadStart(disp1);
        Thread t = new Thread(ts1);
        t.Start();

    }
}

dotnetfiddle

Chris Wong
  • 564
  • 4
  • 4
-1

If you're going to be passing a parameter to your static method you need to use a ParamaterizedThreadStart

class Class1
{
    public static void Disp(object num)
    {

            try
            {
                Console.WriteLine((int)num);
                Thread.Sleep(500);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR");
            }

        Console.WriteLine("Done");


    }

    public static void Main(string[] args)
    {
        Thread t = new Thread(Class1.Disp);
        t.Start(1);
        Console.ReadLine();

    }
}

}

Caius Jard
  • 72,509
  • 5
  • 49
  • 80