线程与多参数线程、参数

2023-09-02 10:24:42 作者:斯文先生

有谁知道如何将多个参数传递到Thread.Start程序?

Does anyone know how to pass multiple parameters into a Thread.Start routine?

我想扩展类,但C#Thread类是密封的。

I thought of extending the class, but the C# Thread class is sealed.

下面是我认为的code看起来像:

Here is what I think the code would look like:

...
    Thread standardTCPServerThread = new Thread(startSocketServerAsThread);

    standardServerThread.Start( orchestrator, initializeMemberBalance, arg, 60000);
...
}

static void startSocketServerAsThread(ServiceOrchestrator orchestrator, List<int> memberBalances, string arg, int port)
{
  startSocketServer(orchestrator, memberBalances, arg, port);
}

顺便说一句,我开始了一些不同的协作型,平衡和端口线程。请考虑线程安全性也。

BTW, I start a number of threads with different orchestrators, balances and ports. Please consider thread safety also.

推荐答案

尝试使用一个lambda EX pression捕捉到的参数。

Try using a lambda expression to capture the arguments.

Thread standardTCPServerThread = 
  new Thread(
    unused => startSocketServerAsThread(initializeMemberBalance, arg, 60000)
  );