C# - ThreadPool QueueUserWorkItem 使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17419961/
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
C# - ThreadPool QueueUserWorkItem Use?
提问by Bitterblue
Just right now I'm using following code to add queued threads. I don't like it. And my colleagues won't either because they don't know C# very well. All I want is of course to queue a method to be executed in a new thread.
现在我正在使用以下代码添加排队线程。我不喜欢。我的同事也不会,因为他们不太了解 C#。我想要的当然是将要在新线程中执行的方法排队。
private static void doStuff(string parameter)
{
// does stuff
}
// call (a)
ThreadPool.QueueUserWorkItem(a => doStuff("hello world"));
// call (b)
ThreadPool.QueueUserWorkItem(delegate { doStuff("hello world"); });
So are there other use variations of ThreadPool.QueueUserWorkItem?
那么还有其他使用变化ThreadPool.QueueUserWorkItem吗?
Best would be another 1-Line-Call. If possible with use of Func<>or Action<>.
最好是另一个 1-Line-Call。如果可能,使用Func<>或Action<>。
EDIT:编辑:从答案和评论中得到(b),我已经更喜欢它了。
采纳答案by ilansch
The answer for your question depends on how you design the application. Do you put it inside a common project ? you dont want to overhead a simple operations.
您的问题的答案取决于您如何设计应用程序。你把它放在一个共同的项目中吗?你不想开销一个简单的操作。
But, You could create a generic call for ThreadPool QueueUserItem that receive params, 1 param, 2 param, etc.. This is good instead of sending a simple string and be restricted.
但是,您可以为接收参数、1 参数、2 参数等的 ThreadPool QueueUserItem 创建一个通用调用。这很好,而不是发送一个简单的字符串并受到限制。
This how you impl a parameters QueueUserItem with WaitCallback:
这是您如何使用 WaitCallback 实现参数 QueueUserItem 的方式:
ThreadPool.QueueUserWorkItem(
new WaitCallback(delegate(object state)
{ YourMethod(Param1, Param2, Param3); }), null);
taken from C# Execute Method (with Parameters) with ThreadPool
And some links for ideas:
http://msdn.microsoft.com/en-us/library/4yd16hza.aspx
Generic ThreadPool in .NET
Difference between delegate.BeginInvoke and using ThreadPool threads in C#
和一些想法的链接:
http: //msdn.microsoft.com/en-us/library/4yd16hza.aspx
.NET 中的通用线程池
delegate.BeginInvoke 和在 C# 中使用 ThreadPool 线程之间的区别
回答by Matthew Watson
I'm not entirely sure what kind of syntax you're looking for, but if you don't like the unused ain your example, why not use Taskinstead?
我不完全确定您正在寻找什么样的语法,但是如果您不喜欢a示例中的未使用,为什么不Task改用呢?
Task.Run(() => doStuff("hello world"));
It doesn't really seem a lot better, but at least it doesn't have an unused identifier.
它看起来并没有好多少,但至少它没有未使用的标识符。
Note: Task.Run()is .Net 4.5 or later. If you're using .Net 4 you have to do:
注意:Task.Run()是 .Net 4.5 或更高版本。如果您使用 .Net 4,则必须执行以下操作:
Task.Factory.StartNew(() => doStuff("hello world"));
which isn't as short.
没有那么短。
Both of the above do use the thread pool.
以上都使用了线程池。
If you really must avoid using a lambda, you can use an anonymous delegate (which @nowhewhomustnotbenamed already mentioned):
如果你真的必须避免使用 lambda,你可以使用匿名委托(@nowhewhomustnotbenamed 已经提到过):
Task.Run(delegate { doStuff("Hello, World!"); });
But what's the point of that? It's much less readable!
但这有什么意义呢?它的可读性要低得多!
回答by Mike de Klerk
What about this?
那这个呢?
class Program
{
static void Main(string[] args)
{
ThreadPool.QueueUserWorkItem(MyWork, "text");
Console.ReadKey();
}
private static void MyWork(object argument)
{
Console.WriteLine("Argument: " + argument);
}
}
Or if you do not want to be signature restrictive and have a simple way of putting methods on a thread you can do it like this.For methods that do and do not return a value and have up to 6 parameters it would require you to define 12 overloads if I am not mistaken. It requires a bit more work up front, but is more simple to use.
或者,如果您不想受到签名限制并且有一种将方法放在线程上的简单方法,您可以这样做。对于返回值和不返回值且最多有 6 个参数的方法,需要您定义如果我没记错的话,12 次重载。它需要更多的前期工作,但使用起来更简单。
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
myClass.DoWork();
Console.ReadKey();
}
}
public static class ObjectThreadExtension
{
public static void OnThread(this object @object, Action action)
{
ThreadPool.QueueUserWorkItem(state =>
{
action();
});
}
public static void OnThread<T>(this object @object, Action<T> action, T argument)
{
ThreadPool.QueueUserWorkItem(state =>
{
action(argument);
});
}
}
public class MyClass
{
private void MyMethod()
{
Console.WriteLine("I could have been put on a thread if you like.");
}
private void MySecondMethod(string argument)
{
Console.WriteLine(argument);
}
public void DoWork()
{
this.OnThread(MyMethod);
this.OnThread(MySecondMethod, "My argument");
}
}

