.net 如何在单独的线程中运行方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5432904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run method in separate thread
提问by Thomas
i found a very good piece of code which run all method in separate thread. the code as follows
我发现了一段非常好的代码,它在单独的线程中运行所有方法。代码如下
private static void Method1()
{
//Method1 implementation
}
private static void Method2()
{
//Method2 implementation
}
private static void RunMethodInSeparateThread(Action action)
{
var thread = new Thread(new ThreadStart(action));
thread.Start();
}
static void Main(string[] args)
{
RunMethodInSeparateThread(Method1);
RunMethodInSeparateThread(Method2);
}
in this case how could i pass parameter to method and also there could be situation where Method1 may need 2 parameter and where Method2 may need 3 parameter. in this situation how to construct RunMethodInSeparateThread in generic way which will accept many param and pass to the method. please help me with code. thanks
在这种情况下,我如何将参数传递给方法,并且可能会出现 Method1 可能需要 2 个参数而 Method2 可能需要 3 个参数的情况。在这种情况下,如何以通用方式构造 RunMethodInSeparateThread,它会接受许多参数并传递给方法。请帮我写代码。谢谢
回答by FreeAsInBeer
To run some code in another thread you could do:
要在另一个线程中运行一些代码,您可以执行以下操作:
new Thread(delegate () {
Method1(param1, param2);
}).Start();
You could accept a collection of parameters or a dictionary for your methods that need to accept a variable number of parameters. Or you could create separate methods that allow a different number of parameters. For example:
对于需要接受可变数量参数的方法,您可以接受一组参数或字典。或者您可以创建允许不同数量参数的单独方法。例如:
private static void Method1()
{
//Method1 implementation
}
private static void Method1(int Param1)
{
//Method1 implementation
}
private static void Method1(int Param1, int Param2)
{
//Method1 implementation
}
回答by Dunc
With .NET 4, your RunMethodInSeparateThreadmethod seems a bit redundant in my opinion. I would just do this:
使用 .NET 4,RunMethodInSeparateThread我认为您的方法似乎有点多余。我只会这样做:
Task.Factory.StartNew(() => { Method1(param1); });
Task.Factory.StartNew(() => { Method2(param1, param2); });
回答by JohnC
If Method1 and Method2 are fairly short-running the best way to do this is not to create a new thread. You can use the .NET thread pool instead, like this:
如果方法 1 和方法 2 运行时间相当短,最好的方法就是不要创建新线程。您可以改用 .NET 线程池,如下所示:
private static void Method1(int x)
{
//Method1 implementation
}
private static void Method2(int x, int y)
{
//Method2 implementation
}
public static void Main()
{
ThreadPool.QueueUserWorkItem(() => Method1(4));
ThreadPool.QueueUserWorkItem(() => Metho2(13, 7));
}
回答by jgauffin
private static void Method1(int x, int y, int c)
{
//Method1 implementation
}
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(state => Method1(1,2,3));
}
回答by AMissico
Is a "Data Slot" helpful? See "Thread Local Storage: Thread-Relative Static Fields and Data Slots" at http://msdn.microsoft.com/en-us/library/6sby1byh.aspx
“数据槽”有用吗?请参阅http://msdn.microsoft.com/en-us/library/6sby1byh.aspx 上的“线程本地存储:线程相关静态字段和数据槽”

