Await a void (Async CTP)

I am really enjoying getting more and more into the new async ctp. Something that stumped me up to now, was how do I run a void method asynchronously.

For instance, suppose I have the following workflow that I want to run.

    private void DoWorkFlowSync()
    {
        txtStatus.Text = "Step 1";
        Step1Workflow();
        txtStatus.Text = "Step 2";
        Step2Workflow();
        txtStatus.Text = "Step 3";
        Step3Workflow();
        txtStatus.Text = "Done";
    }
    
    private void Step1Workflow()
    {            
        Thread.Sleep(1000);             
    }

    private void Step2Workflow()
    {
        Thread.Sleep(1000);            
    }

    private void Step3Workflow()
    {
        Thread.Sleep(1000);            
    }

This is it in its synchronous state, if I try and run it asynchronously my initial attempts would be to code the DoWorkFlowSync as follows.

    private async void DoWorkFlowAsync()
    {
        txtStatus.Text = "Step 1";
        await Step1Workflow();
        txtStatus.Text = "Step 2";
        await Step2Workflow();
        txtStatus.Text = "Step 3";
        await Step3Workflow();
        txtStatus.Text = "Done";
    }

To which the compiler would respond with a compile time error with the following…

Error    1    Cannot await 'void’

So, a bit of a dilemma. I don’t want to have to rewrite each individual method (i.e. Step1Wroflow) to be of type Task. It would be doable if this was a small project but I want to be able to refactor large projects with appropriate asynchronous calls without going to deep into the project. Eventually I made a mind shift and found the following to work.

    private async void DoWorkFlowAsync()
    {
        txtStatus.Text = "Step 1";
        await TaskEx.Run(() => Step1Workflow());
        txtStatus.Text = "Step 2";
        await TaskEx.Run(() => Step2Workflow());
        txtStatus.Text = "Step 3";
        await TaskEx.Run(() => Step3Workflow());
        txtStatus.Text = "Done";
    }

This worked perfectly, the UI was responsive and I have a workflow that is easy to read.

For those that are interested in learning more about the new async ctp go to the forum

This article is part of the GWB Archives. Original Author: MarkPearl

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.