返回从线程价值?

编程入门 行业动态 更新时间:2024-10-23 19:18:48
本文介绍了返回从线程价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我如何从一个线程返回一个值?

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 }

更多推荐

返回从线程价值?

本文发布于:2023-10-25 08:14:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   价值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!