返回从线程价值? [英] Returning a value from thread?
本文介绍了返回从线程价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何从一个线程返回一个值?
How do I return a value from a thread?
推荐答案
一,以获得从一个线程返回值的最简单的方法是使用闭包。创建将举行从线程的返回值,然后捕获它的lambda EX pression的变量。从辅助线程分配回报值这个变量,然后一旦线头可以从父线程使用它。
One of the easiest ways to get a return value from a thread is to use closures. Create a variable that will hold the return value from the thread and then capture it in a lambda expression. Assign the "return" value to this variable from the worker thread and then once that thread ends you can use it from the parent thread.
void Main()
{
object value = null; // Used to store the return value
var thread = new Thread(
() =>
{
value = "Hello World"; // Publish the return value
});
thread.Start();
thread.Join();
Console.WriteLine(value); // Use the return value here
}
这篇关于返回从线程价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文