带输入参数的函数的多线程#

时间:2019-03-22 08:11:20

标签: c# multithreading

我以这种方式使用多线程没有问题

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var t1 = new Thread(new ThreadStart(fun1));
            t1.Start();
            var t2 = new Thread(new ThreadStart(fun2));
            t2.Start();
            Console.ReadKey();
        }
        public static void fun1()
        {
            for (int i = 1; i < 51; i++)
            {
                Console.WriteLine($"i is {i}");
            }
        }
        public static void fun2()
        {
            for (int j = 1; j < 51; j++)
            {
                Console.WriteLine($"j is {j}");
            }
        }
    }

}

很明显,该函数没有输入参数。但是当我想使用一个甚至有一个参数的函数时,都会遇到错误。

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var t1 = new Thread(new ThreadStart(fun1("str1")));
            t1.Start();
            var t2 = new Thread(new ThreadStart(fun2("str2")));
            t2.Start();
            Console.ReadKey();
        }
        public static void fun1(string x)
        {
            for (int i = 1; i < 51; i++)
            {
                Console.WriteLine($"i is {i}");
            }
        }
        public static void fun2(string y)
        {
            for (int j = 1; j < 51; j++)
            {
                Console.WriteLine($"j is {j}");
            }
        }
    }

}

所以请告诉我如何在多线程中使用带有参数(作为输入)的函数?

2 个答案:

答案 0 :(得分:0)

尝试执行以下操作:

    string str = "string";
        var t1 = new Thread(
         o => 
        {
            fun1((string)o);    
        });
        t1.Start(str);

和fun2一样。

答案 1 :(得分:0)

有关ParameterizedThreadStart Delegate的更多信息,请参考this MSDN link,以了解如何使用线程将参数传递给方法:

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        newThread.Start(42);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        newThread.Start("The answer.");
    }

    public static void DoWork(object data)
    {
        Console.WriteLine("Static thread procedure. Data='{0}'",
            data);
    }

    public void DoMoreWork(object data)
    {
        Console.WriteLine("Instance thread procedure. Data='{0}'",
            data);
    }
}
// This example displays output like the following:
//       Static thread procedure. Data='42'
//       Instance thread procedure. Data='The answer.'

要传递multiple parameters,您可以创建一个具有属性的类,并传递该类中填充了属性值的对象,并在线程方法中访问该类对象,如下所示:

using System;
using System.Threading;

public class Work
{
    public static void Main()
    {
        // Start a thread that calls a parameterized static method.
        Thread newThread = new Thread(Work.DoWork);
        //create a class to hold multiple params
        DataClass objData = new DataClass();
        objData.Param1 = "value 1";
        objData.Param2 = "value 2";

        newThread.Start(objData);

        // Start a thread that calls a parameterized instance method.
        Work w = new Work();
        newThread = new Thread(w.DoMoreWork);
        newThread.Start(objData);
    }

    public static void DoWork(object data)
    {
        DataClass myData = (DataClass)data;
        Console.WriteLine("Static thread procedure. Data Param1='{0}'",
            data.Param1);
    }

    public void DoMoreWork(object data)
    {
        DataClass myData = (DataClass)data;
        Console.WriteLine("Instance thread procedure. Data Param2='{0}'",
            data.Param2);
    }
}