Sency
Sency

Reputation: 2878

C#5 async method completed event.

I have an asynchronous method like this

public async void Method()
{
    await // Long run method
}

When Im calling this method can I have a event when this Method completed?

public void CallMethod()
{
    Method();
    // Here I need an event once the Method() finished its process and returned.
} 

Upvotes: 3

Views: 4034

Answers (1)

usr
usr

Reputation: 171246

Why do you need that? Do you need to wait for completion? That works like this:

public async Task Method() //returns Task
{
    await // Long run method
}

public void CallMethod()
{
    var task = Method();

    //here you can set up an "event handler" for the task completion
    task.ContinueWith(...);

    await task; //or await directly
} 

If you cannot use await and really need to use an event-like pattern, use ContinueWith. You can think of that as adding an event handler for task completion.

Upvotes: 8

Related Questions