螺纹返回类型 [英] thread with return type

查看:138
本文介绍了螺纹返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有它返回一个布尔值的方法。 我想通过

I have a method which returns a bool value. I want to execute that method through Thread.

发T1 =新主题(新的ThreadStart(doThis));

能否请您提出一个方法来获取返回的值?

Could you please suggest a way to get that returned value?

推荐答案

在理想情况下,可以使用任务并行库和任务< T> ,而不是...但除此之外,你将需要建立某种形式的共享变量的重新present的结果;当线程完成,读取任何线索,你需要它的结果。

Ideally, use the Tasks Parallel Library and Task<T> instead... but otherwise, you'll need to set up some sort of shared variable to represent the result; when the thread has finished, read the result from whatever thread you need it in.

另一种方法是使用委托返回一个布尔和呼叫的BeginInvoke 上代表就执行它线程池,返回一个的IAsyncResult ,这将让你等待结果。

Another alternative is to use a delegate which returns a bool and call BeginInvoke on that delegate to execute it on the thread-pool, returning an IAsyncResult which will allow you to wait for the result.

这篇关于螺纹返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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