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

前端 未结 6 1875
悲哀的现实
悲哀的现实 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:14

    Is something like this what you wanted?

    // begin execution asynchronously
    IAsyncResult result = myObject.BeginInvoke("data.dat", null, null);
    
    // wait for it to complete
    while (result.IsCompleted == false) {
       // do some work
       Thread.Sleep(10);
       }
    
    // get the return value
    int returnValue = myObject.EndInvoke(result);
    

提交回复
热议问题