Wait IAsyncResult function until complete
Asked Answered
C

2

5

I need to create mechanism which waits IAsycnResult method till complete. How can i achieve this ?

IAsyncResult result = _contactGroupServices.BeginDeleteContact(
  contactToRemove.Uri,
  ar =>
      {
      try
         {
           _contactGroupServices.EndDeleteContact(ar);
         }
      catch (RealTimeException rtex)
         {
            Console.WriteLine(rtex);
         }
      },
      null ); 
Catima answered 22/5, 2015 at 7:51 Comment(6)
You can finish you method by .Result or .Wait(), or use await if you're in async methodKilkenny
this? result.AsyncWaitHandle.WaitOne();Witch
I am not in async method . How can i do otherwise ?Kkt
result.AsyncWaitHandle.WaitOne() didnt help me. I tried this.Kkt
@FatihÜstdağ check out code from my answer. I hope it is usefull for youWitch
You shouldn't be using asynchronous operations in the first place if you're just going to wait on them.Gerstner
W
3

I dont have any idea what is the goal of this method _contactGroupServices.BeginDeleteContact . But lets try to write template for it

Func<object> doWork = () =>
{
    return _contactGroupServices.BeginDeleteContact(<your parameters>)

};


AsyncCallback onWorkDone = (ar) =>
{
    //work done implementation
};

IAsyncResult asyncResult = doWork.BeginInvoke(onWorkDone, null); 

asyncResult.AsyncWaitHandle.WaitOne();

var result = doWork.EndInvoke(asyncResult);
Witch answered 22/5, 2015 at 8:4 Comment(0)
M
6

The task factory has a method just for that: Task.Factory.FromAsync.

var task = Task.Factory.FromAsync(
    contactGroupServices.BeginDeleteContact(contactToRemove.Uri),
    ar =>
    {
        try
        {
            _contactGroupServices.EndDeleteContact(ar);
        }
        catch (RealTimeException rtex)
        {
            Console.WriteLine(rtex);
        }
    });

You can block until it's done by calling task.Wait, but that will deadlock in most cases (if called from a UI thread, for example).

You might want to put it inside an asynchronous method:

async Task M()
{
    ...
    await Task.Factory.FromAsync(
        contactGroupServices.BeginDeleteContact(contactToRemove.Uri),
        ar =>
        {
            try
            {
                _contactGroupServices.EndDeleteContact(ar);
            }
            catch (RealTimeException rtex)
            {
                Console.WriteLine(rtex);
            }
        });
    ...
}
Mooneye answered 22/5, 2015 at 15:58 Comment(0)
W
3

I dont have any idea what is the goal of this method _contactGroupServices.BeginDeleteContact . But lets try to write template for it

Func<object> doWork = () =>
{
    return _contactGroupServices.BeginDeleteContact(<your parameters>)

};


AsyncCallback onWorkDone = (ar) =>
{
    //work done implementation
};

IAsyncResult asyncResult = doWork.BeginInvoke(onWorkDone, null); 

asyncResult.AsyncWaitHandle.WaitOne();

var result = doWork.EndInvoke(asyncResult);
Witch answered 22/5, 2015 at 8:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.