开发者

Quick Multithreading Question

I have a startup function that calls a function which returns a boolean based on whether the set up is successful or not. True if successful, false if failed. I would like to start that function on a new thread and then check the status of the function: here is the code.

System.Threadi开发者_C百科ng.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(StartAdapter));
thread.Start();

My question is, how in that case would I check the return status of the startadapter method? Because my friend told me that I will not know the return status because it is started on another thread, and yet trying:

System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(StartAdapter));
thread.Start();
bool result = StartAdapter();

would call the function twice, which is something I don't want either. Does anybody have some insight into this?

How in this case would I check the boolean returned from the startadapter function?

.NET 3.5


for this case there is the Task<T> class that executes on the ThreadPool (for example) and lets you know the return value after it's finished

just use:



var task = TaskFactory<yourResultType>.StartNew(StartAdapter);
Action<yourResultType> actionAfterResult = ...; // whatever you have to do
task.ContinueWith(actionAfterResult);

// or:
var result = task.Result; // this will block till the result is computed

// or another one of the alternatives you can learn about on MSDN (see link above)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜