C# 动态创建线程

C# 动态创建线程,c#,multithreading,C#,Multithreading,我知道如何使用线程和后台工作程序,但只能以“静态”的方式使用(因此对它们进行硬编码)。但是我想用这样的东西 public static void StartThread(string _Method) { Thread t = new Thread(() => _Method; t.Start(); } 我知道这会失败,因为\u Method是一个字符串。我已经阅读了使用委托的内容,但我不确定这将如何工作,以及在这种情况下是否需要它 我想在需要时启动特定函数的线程(以便动

我知道如何使用线程和后台工作程序,但只能以“静态”的方式使用(因此对它们进行硬编码)。但是我想用这样的东西

public static void StartThread(string _Method)
{
    Thread t = new Thread(() => _Method;
    t.Start();
}
我知道这会失败,因为
\u Method
是一个字符串。我已经阅读了使用
委托
的内容,但我不确定这将如何工作,以及在这种情况下是否需要它

我想在需要时启动特定函数的线程(以便动态创建线程)。

您可以这样做:

public static void StartThread(string s)
{
    Thread t = new Thread(() => { Console.WriteLine(s); });
    t.Start();
}
或者,如果您希望从外部设置方法:

public static void StartThread(Action p)
{
    Thread t = new Thread(p);
    t.Start();
}

...
StartThread(() => { Console.WriteLine("hello world"); });
编辑:
事实上,你确实需要

public static void StartThread(Action p)
{
    Thread t = new Thread(new ThreadStart(p));
    t.Start();
}
如果您想在不同的线程上分割工作,您可以使用它。否则,按照Vlad的答案,使用接受委托的方法

任务 线
要创建动态线程,您需要在运行时创建它们。在本例中,我创建了一个方法CreateThreads,该方法创建方法并将它们添加到线程列表中。稍后,我有一个方法StartMyThreads来启动所有线程。我希望这就是你想要的

    List<Thread> lstThreads = new List<Thread>();
    public void CreateThreads()
    {
        Thread th = new Thread(() => { MyThreadFun(); });
        lstThreads.Add(th);
    }

    private void MyThreadFun()
    {
        Console.WriteLine("Hello");
    }

    private void StartMyThreads()
    {
        foreach (Thread th in lstThreads)
            th.Start();
    }
List lsthreads=new List();
public void CreateThreads()
{
Thread th=新线程(()=>{MyThreadFun();});
添加(th);
}
私人图书馆
{
Console.WriteLine(“你好”);
}
私有void StartMyThreads()
{
foreach(螺纹中的th螺纹)
th.Start();
}

如果您决定使用TPL任务,请注意TPL使用
线程池
线程,因此在某些情况下,它与手动创建的
线程
相比会有所不同。所以,请阅读文档,确保您知道在做出这样的决定时您会做什么


您可能会发现这很有用:

谢谢Vlad,是的,我想从“外部”设置它。因此,使用后一种代码,但Visual Studio引发了两个错误:
参数1:无法从“System.Action”转换为“System.Threading.ParameterizedThreadStart”
与“System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)”匹配的最佳重载方法有一些无效参数
。谢谢,然而,你的代码也给了我和Vlad一样的错误<代码>参数1:无法从“System.Action”转换为“System.Threading.ParameterizedThreadStart”和
与“System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)”匹配的最佳重载方法具有一些无效参数
。谢谢,效果非常好!我正在启动这样的方法
Utils.StartThread(()=>{Utils.Debug(“Hi”);})Thread-Thread=new-Thread(new-ThreadStart(action))并避免再创建一个lambda。@Felix:试试看!顺便说一句,您通常也可以省略创建操作:
Start(MyMethodToStart)而不是
启动(新操作(MyMethodToStart))
@Vlad这是我知道的,但我想让它对读者保持干净。我想动态创建它们,这只能用于硬编码。我刚刚更改了答案,看看你可以写它var thread=new thread(()=>Console.WriteLine(“test”);或者var thread=new thread(参数=>Console.WriteLine(参数));除非你只是想让其他初级程序员明白你的代码,否则我不明白你为什么要这样写。我想知道这样创建线程是否意味着需要一个线程池?
public static Thread Start(Action action) {
    Thread thread = new Thread(() => { action(); });
    thread.Start();
    return thread;
}

// Usage
Thread t = Start(() => { ... });
// You can cancel or join the thread here

// Or use a method
Start(new Action(MyMethodToStart));
    List<Thread> lstThreads = new List<Thread>();
    public void CreateThreads()
    {
        Thread th = new Thread(() => { MyThreadFun(); });
        lstThreads.Add(th);
    }

    private void MyThreadFun()
    {
        Console.WriteLine("Hello");
    }

    private void StartMyThreads()
    {
        foreach (Thread th in lstThreads)
            th.Start();
    }
var thread = new Thread(
    (ThreadStart)delegate
        {
            Console.WriteLine("test");
        });
var thread = new Thread(
    (ParameterizedThreadStart)delegate(object parameter)
        {
            Console.WriteLine(parameter);
        });