多参数线程 [英] thread with multiple parameters

查看:28
本文介绍了多参数线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何将多个参数传递给 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.

我认为代码如下所示:

...
    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 表达式来捕获参数.

Try using a lambda expression to capture the arguments.

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

这篇关于多参数线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆