如何当一个异步/ awaitable任务运行轨道轨道、任务、awaitable

2023-09-02 11:59:00 作者:在太平间里看惊悚片

我试图从基于事件的异步模式,我跟踪使用唯一的ID和asynoperationmanager运行方式过渡。 由于这现在已经下降,从Windows 8的应用程序,我试图让与异步/等待同样的效果,但不能完全弄清楚如何。 我正在试图实现是一样的东西。

 专用异步任务updateSomething()
{
    如果(***该方法已在运行***)
    {
        runagain = TRUE;
    }
    其他
    {
        等待的someMethod();
        如果(runagain)
        {
            再次运行方法
        }
    }
}
 

我挣扎是查不到,如果该方法正在运行的一部分。我试着创建任务,并期待在双方的状态和异步方法的.STATUS,但他们似乎并没有把眼光正确的位置。 谢谢

更新:这是当前code我用的是.NET 4达到相同的结果。 _updateMetaDataAsync的基础上,基于事件的异步模式的类。

 私人无效updateMetaData()
    {
        如果(_updateMetaDataAsync.IsTaskRunning(_updateMetaDataGuid_CheckAllFiles))
        {
            _updateMetaDataGuid_CheckAllFiles_Again = TRUE;
        }
        其他
        {
            _updateMetaDataGuid_CheckAllFiles_Again = FALSE;
            _updateMetaDataAsync.UpdateMetaDataAsync(_updateMetaDataGuid_CheckAllFiles);
        }
    }

私人无效updateMetaDataCompleted(对象发件人,UpdateMetaDataCompletedEventArgs E)
    {
        如果(_updateMetaDataGuid_CheckAllFiles_Again)
        {
            updateMetaData();
        }
    }
 

解决方案

异步 / 伺机本身是为了被用来创建从UI线程异步执行顺序操作。你可以把它做并行运算,但一般的操作加入回UI线程与某种结果。 (另外还有做发射后不管类型的​​异步操作与等待的可能性,但不推荐)。即没有什么固有的异步 / 等待来支持进度报告。

Python网络爬虫的同步和异步

您的可以的得到进步了使用code 异步 / 等待 ;但你需要使用像 IProgress&LT新进展接口; T> 。有关进展情况的详细信息与报告异步 / 伺机,见http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx.迁移到这应该仅仅是调用一个 IProgress 委托,而不是进展事件的问题。

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();
        }
    }

解决方案

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.