如何从Task返回结果?

时间:2015-12-09 09:34:44

标签: c# multithreading

我有以下方法:

public int getData() { return 2; } // suppose it is slow and takes 20 sec

// pseudocode
public int GetPreviousData()
{
    Task<int> t = new Task<int>(() => getData());
    return _cachedData; // some previous value
    _cachedData = t.Result; // _cachedData == 2
}

我不想等待已经运行的操作的结果。

我想返回_cachedData并在Task完成后更新。

怎么做?我正在使用.net framework 4.5.2

3 个答案:

答案 0 :(得分:7)

您可能希望在此处使用out参数:

public Task<int> GetPreviousDataAsync(out int cachedData)
{
    Task<int> t = Task.Run(() => getData());
    cachedData = _cachedData; // some previous value
    return t; // _cachedData == 2
}

int cachedData;
cachedData = await GetPreviousDataAsync(out int cachedData);

注意Task.Run事情:这会使用线程池启动任务并返回Task<int>以让调用者决定是否应该等待,继续或触发并忘记< / em>它。

请参阅以下示例。我已将所有内容重新安排到课堂上了:

class A
{
    private int _cachedData;
    private readonly static AutoResetEvent _getDataResetEvent = new AutoResetEvent(true);

    private int GetData()
    {
        return 1;
    }

    public Task<int> GetPreviousDataAsync(out int cachedData)
    {
        // This will force calls to this method to be executed one by one, avoiding
        // N calls to his method update _cachedData class field in an unpredictable way
        // It will try to get a lock in 6 seconds. If it goes beyong 6 seconds it means that 
        // the task is taking too much time. This will prevent a deadlock
        if (!_getDataResetEvent.WaitOne(TimeSpan.FromSeconds(6)))
        {
            throw new InvalidOperationException("Some previous operation is taking too much time");
        }

        // It has acquired an exclusive lock since WaitOne returned true

        Task<int> getDataTask = Task.Run(() => GetData());
        cachedData = _cachedData; // some previous value

        // Once the getDataTask has finished, this will set the 
        // _cachedData class field. Since it's an asynchronous 
        // continuation, the return statement will be hit before the
        // task ends, letting the caller await for the asynchronous
        // operation, while the method was able to output 
        // previous _cachedData using the "out" parameter.
        getDataTask.ContinueWith
        (
            t =>
            {
                if (t.IsCompleted)
                    _cachedData = t.Result;

                // Open the door again to let other calls proceed
                _getDataResetEvent.Set();
            }
        );

        return getDataTask;
    }

    public void DoStuff()
    {
        int previousCachedData;
        // Don't await it, when the underlying task ends, sets
        // _cachedData already. This is like saying "fire and forget it"
        GetPreviousDataAsync(out previousCachedData);
    }
}

答案 1 :(得分:1)

您需要在类中存储缓存的值,然后在查询值时检查您是否已经更新它。如果您只是返回缓存的值。如果没有,则启动新查询并返回缓存值。

答案 2 :(得分:1)

如果您不必等待任务完成,您可以让该功能启动任务并在ContinueWith

中处理设置
    public int GetPreviousData()
    {
        Task.Run((Func<int>)getData).ContinueWith(t => _cachedData = t.Result);            
        return _cachedData; // some previous value
    }

如果竞争条件存在问题,您可以先将_cachedData分配给变量,然后运行任务并立即返回变量,但如果getData需要任何时间,那么这应该不是问题。