How to get return value when BeginInvoke/Invoke is called in C#

前端 未结 6 1874
悲哀的现实
悲哀的现实 2020-12-04 17:57

I\'ve this little method which is supposed to be thread safe. Everything works till i want it to have return value instead of void. How do i get the return value when BeginI

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 18:38

    If you want a return value from you method, you shouldn't be using async version of the method, you should use .Invoke(...). Which is synchronous, that is it will execute your delegate, and won't return until it's complete. In your example as it is now, BeginInvoke will send the request to execute your delegate, and return right away. So there is nothing to return.

提交回复
热议问题