螺纹返回类型螺纹、类型

2023-09-03 05:29:39 作者:活出尼玛的姿态

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

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.