开发者

c# Thread Pool - how to return value (to make it fully synchronized with request)

开发者 https://www.devze.com 2023-04-06 04:51 出处:网络
I have a webservice that ask 开发者_如何转开发for data from my server. The request needs to be run inside a thread pool, how do I return the value to the client?You can\'t pass a response back to you

I have a webservice that ask 开发者_如何转开发for data from my server.

The request needs to be run inside a thread pool, how do I return the value to the client?


You can't pass a response back to your client on the initial request. You would have to return a token that the client would subsequently poll another method on your class with to check to see if the operation was complete; if the operation is complete, the method returns the result, otherwise, it returns a result to indicate that it should continue to poll.

If you are using .NET 4.0, I'd recommend using the Task<T> class, and passing that back to your client; your client can poll for the result, wait on it, get notification when it's done, etc.

If you are not working with .NET 4.0, then I recommend using a custom Delegate, assigning an anonymous method/method group to it, and then calling BeginInvoke on the delegate; this will return an IAsyncResult implementation which you can use to perform the operations mentioned above (polling, waiting, etc).


Multithreading is by its definition asynchronous. In order to get value back, you need to implement some form of callback (a method that the thread can call when it has finished its operation) so that it can pass the value.

0

精彩评论

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

关注公众号