我想异步执行CPU密集型工作。我想使用这样的代码:

   ....
   updateGUIObj = await setSelectedGUICellValueAsync(arg1, arg2, cssId, isInitializeCbxChecked);
   ....


   public IAsyncOperation<UpdateGUIObj> setSelectedGUICellValueAsync(int arg1, int arg2, String cssId, bool isInitializeCbxChecked)
   {
        updateGUIObj = new UpdateGUIObj();

        ....  // compute intensive work setting "updateGUIObj"

        return ?????;
    }

我应该如何编写上面的return语句?

最佳答案

您可以使用 async/await 模式并返回Task<>对象:

...
var updateGUIObj = await setSelectedGUICellValueAsync(arg1, arg2, cssId, isInitializeCbxChecked);
...

public async Task<UpdateGUIObj> setSelectedGUICellValueAsync(int arg1, int arg2, String cssId, bool isInitializeCbxChecked)
{
    var updateGUIObj = new UpdateGUIObj();

    // .... compute intensive work setting "updateGUIObj"

    return updateGUIObj;
}

关于c# - 如何返回IAsyncOperation <TReturn>结果?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35656445/

10-12 02:16