0

Whether the method that is called in ParameterizedThreadStart can use more than one parameter?

class Fungsi
{
    public void satu(int a, int b) 
    {
        for (int x = 0; x <= 20; x++)
        {
            tampil(a, b, '=', x);
            b++;
            Thread.Sleep(1000);
        }
     }
}

class Program
{
    static void Main(string[] args)
    {
        Fungsi a = new Fungsi();
        Console.CursorVisible = false;

        Thread ab = new Thread(new ParameterizedThreadStart(a.satu));
        ab.Start(10,1);

        Console.ReadLine();
    }
}

why Thread ab = new Thread(new ParameterizedThreadStart(a.satu));

how to use ParameterizedThreadStart?

and I want to create 4 Multithread with one method

4
  • 2
    Pass them as an array? Or a class of parameters? Commented Apr 6, 2017 at 9:19
  • You can just use anonymous function: Thread ab = new Thread(() => satu(10, 1)); ab.Start(); Commented Apr 6, 2017 at 9:21
  • Parameters @BugFinder Commented Apr 6, 2017 at 9:36
  • Can one method to create 4 Thread?? Commented Apr 6, 2017 at 9:37

1 Answer 1

1

Given the method

void Worker(string a, int b){}

for .NET 2.0 you need to use delegates:

ThreadStart start = delegate { Worker("bla", 10); };
Thread t = new Thread(start);
t.Start();

for later .NET versions you could use the anonymous function:

Thread t = new Thread(() => Worker("bla", 10));
t.Start();

or since .NET 4 you could use TPL Tasks:

Task t = new TaskFactory().StartNew(() => Worker("bla", 10));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.