New Posts New Posts RSS Feed: VS 2012 Async
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

VS 2012 Async

 Post Reply Post Reply
Author
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Topic: VS 2012 Async
    Posted: 20-Sep-2012 at 8:53am
Just curious as to what the best practices are now for writing async code.

Previously we have had code such as:


  Busy.AddWatch();
                Repository.SaveAsync(() =>
                                         {
                                           Success Logic Here
                                         }, ErrorHandler.HandleError)
                    .WhenCompleted(e => Busy.RemoveWatch());


Now we have code like


                Busy.AddWatch();
                var op =  Repository.SaveAsync().ContinueWith(t => Busy.RemoveWatch());
                op.HandleError(ErrorHandler.HandleError);
                await op;
                Success Logic Here


Is this the best way? I have seen people wrapping try handlers around await calls but that seems messy.

I see that HandleError doesn't return a Task so you can't await it. Is that by design?


Edited by smi-mark - 20-Sep-2012 at 9:11am
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: 20-Sep-2012 at 9:42am
With async/await you write code very much like you would write synchronous code. Error handling is done using try/catch. The following is an example. In this case I'm just rethrowing the exception and handle it in the UnhandledException handler.

        public async void Delete()
        {
            await _dialogManager.ShowMessageAsync(
                string.Format("Are you sure you want to delete {0}?", ActiveStaffingResource.FullName),
                DialogResult.Yes, DialogResult.No, DialogButtons.YesNo);

            try
            {
                using (ActiveDetail.Busy.GetTicket())
                {
                    ActiveUnitOfWork.StaffingResources.Delete(ActiveStaffingResource);
                    await ActiveUnitOfWork.CommitAsync();
                }

                ActiveItem.TryClose();
            }
            catch (TaskCanceledException)
            {
                ActiveUnitOfWork.Rollback();
            }
            catch (Exception)
            {
                ActiveUnitOfWork.Rollback();
                throw;
            }
        }
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: 20-Sep-2012 at 9:46am
Also, here is a useful error handler that I'm using in TempHire v2. 

    public class ErrorHandler : IErrorHandler
    {
        private readonly IDialogManager _dialogManager;

        [ImportingConstructor]
        public ErrorHandler(IDialogManager dialogManager)
        {
            _dialogManager = dialogManager;
        }

        #region IErrorHandler Members

        public void HandleError(Exception ex)
        {
            string customMessage = null;
            if (ex is EntityManagerSaveException &&
                ((EntityManagerSaveException) ex).FailureType == PersistenceFailure.Concurrency)
                customMessage = "Another user has previously saved the current record.";

            if (ex is TaskCanceledException)
            {
                // Log and ignore
                LogFns.DebugWriteLine(ex.Message);
                return;
            }

            _dialogManager.ShowMessageAsync(customMessage ?? ex.Message, DialogButtons.Ok);
        }

        #endregion
    }
Back to Top
smi-mark View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 24-Feb-2009
Location: Dallas, Texas
Posts: 343
Post Options Post Options   Quote smi-mark Quote  Post ReplyReply Direct Link To This Post Posted: 21-Sep-2012 at 6:19am
That works great, thanks. This really helps clean up and simplify the code!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down