New Posts New Posts RSS Feed: Performing Async functions in an Event Handler
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Performing Async functions in an Event Handler

 Post Reply Post Reply
Author
LowOrbit View Drop Down
Newbie
Newbie


Joined: 17-Aug-2007
Location: United States
Posts: 20
Post Options Post Options   Quote LowOrbit Quote  Post ReplyReply Direct Link To This Post Topic: Performing Async functions in an Event Handler
    Posted: 13-Mar-2013 at 8:24am
Hello
 
I am looking for advice on how to perform Async functions (i.e. Saving or Querying) within an event handler of a UI control. For example, I have a gridview and I need to perform validation and saving of a specific row before the user can move off the row.
 
The problem is that the async calls start and execution continues with the rest of the method and exits before the async method actually completes. There may be times when a validation error occurs during the save and I want to prevent focus from leaving the row but since the grid has already allowed the user to move focus, it's too late to move it back.  
 
A lot of event handlers have a way to signal back or return something to the sender to indicate succss or failure of an event (i.e. cancel a click event or something like that).
 
So how can I perform a async call in an event handler and get the response back from the async method before exiting the handler?
 
Thanks!
 
 
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 13-Mar-2013 at 9:22am
Unless the event gives you the ability to signal back via the event args, you can't do this. .NET events are synchronous by nature. The UI control in this case has to support asynchronicity by providing callback means via the event args. If this particular event doesn't have it, than you are out of luck.

You can make an event handler async by using the async keyword, but the handler will immediately return upon the first await statement. Asynchronous methods are non-blocking methods for a very good reason. They simply pause execution whenever there is an await and resume execution once the awaited task completes, but the method itself immediately returns to the caller in order to free up the UI thread. 

So, in principal this is how you would write such an event handler if the control has means to let it know when you are done.

public async void SomeEventHandler(object sender, SomeEventArgs args)
{
     await SomeAsyncMethod();

     args.Done();   // Provided there is such a thing for this event
}

Now having said that, you can block the UI thread while waiting for a task to complete, but you shouldn't do that. Blocking the UI defeats the whole point of asynchronous programming in the first place. 

public void SomeEventHandler(object sender, SomeEventArgs args)
{
     SomeAsyncMethod().Wait();   // Block thread until task completes, but really don't do this ever!!!!!
}
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 16-Mar-2013 at 5:44am
What I do is first do field level validation so that the user is notified of validation errors when he moves off the field. Then when the user moves off the row, do row level validation. If it fails, I just move the current row back to the former row so the user can handle the row level validation error. If the validation is fast, it appears to the user he never left the row.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down