Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track if an async/awaitable task is running

Tags:

c#

.net

I'm trying to transition from the Event-based Asynchronous Pattern where I tracked running methods using unique id's and the asynoperationmanager.
As this has now been dropped from Windows 8 Apps I'm trying to get a similar effect with Async/Await but can't quite figure out how.
What I'm trying to achieve is something like

private async Task updateSomething()
{
    if(***the method is already running***)
    {
        runagain = true;
    }
    else
    {
        await someMethod();
        if (runagain)
        {
            run the method again
        }            
    }
}

The part I'm struggling with is finding out if the method is running. I've tried creating a Task and looking at the status of both that and the .status of the async method but they don't appear to be the correct place to look. Thanks

UPDATE: This is the current code I use in .net 4 to achieve the same result. _updateMetaDataAsync is a class based on the Event-Based Asynchronous Pattern.

private void updateMetaData()
    {
        if (_updateMetaDataAsync.IsTaskRunning(_updateMetaDataGuid_CheckAllFiles))
        {
            _updateMetaDataGuid_CheckAllFiles_Again = true;
        }
        else
        {
            _updateMetaDataGuid_CheckAllFiles_Again = false;
            _updateMetaDataAsync.UpdateMetaDataAsync(_updateMetaDataGuid_CheckAllFiles);
        }
    }

private void updateMetaDataCompleted(object sender, UpdateMetaDataCompletedEventArgs e)
    {
        if (_updateMetaDataGuid_CheckAllFiles_Again)
        {
            updateMetaData();
        }
    }
like image 490
Oli Avatar asked Aug 23 '12 18:08

Oli


2 Answers

If you're using a Task you've created, you can check the Task's Status property (or just see Task.IsCompleted if completion is the only state you are interested in).

That being said, await will not "return" until the operation either completes, raises an exception, or cancels. You can basically safely assume that, if you're still waiting on the "await", your task hasn't completed.

like image 161
Reed Copsey Avatar answered Oct 13 '22 01:10

Reed Copsey


async/await itself is intended to be used to create sequential operations executed asynchronously from the UI thread. You can get it to do parallel operations, but generally the operations "join" back to the UI thread with some sort of result. (there's also the possibility of doing "fire-and-forget" types of asynchronous operations with await but it's not recommended). i.e. there's nothing inherent to async/await to support progress reporting.

You can get progress out of code using async/await; but you need to use new progress interfaces like IProgress<T>. For more info on progress reporting with async/await, see http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx. Migrating to this should just be a matter of calling an IProgress delegate instead of a Progress event.

like image 40
Peter Ritchie Avatar answered Oct 12 '22 23:10

Peter Ritchie