在异步任务中异步执行多个任务

时间:2014-06-04 14:43:41

标签: c# wcf asynchronous task

我有以下WCF端点:

    public string AddPlace(Place placeBounds, ...)
    {

        FetchAllSocialPlatforms(placeBounds);

        Place newPlace = PlaceDB.AddPlace(placeBounds, ...);

        return newPlace;
    }

...其中FetchAllSocialPlatforms()是:

    private Task FetchAllSocialPlatforms(Place bounds)
    {
        try
        {
            Task<int> taskTwitter = FetchTwitter(bounds, user);
            Task<int> taskInstagram = FetchInstagram(bounds, user);
            Task<int> taskFlickr = FetchFlickr(bounds, user);
            Task<int> taskYouTube = FetchYouTube(bounds, user);

            Task.WhenAll(taskTwitter, taskInstagram, taskFlickr, taskYouTube);

            SendNotificationOnComplete(...); // Azure Service Bus / SignalR message back to app

        }
        catch (Exception ex)
        {
            Log.Error(...);
        }

        return null;
    }

我的目的是将newPlace返回给调用者(在这种情况下,它是JavaScript Web应用程序),而FetchAllSocialPlatforms()正在服务器端的后台工作。完成从所有平台的所有提取后,将发送通知。

到目前为止,我的代码是同步运行而不是异步运行,并且在完成所有提取之前不会返回newPlace

我意识到我没有把头完全包裹在任务中并等待操作,我很想知道我做错了什么。

感谢。

编辑#1:

我被要求显示更多代码实现,所以这里是:

    private async Task<int> FetchTwitter(Place bounds)
    {
        int count = 0;
        try
        {
            count = Fetch(bounds, PlatformType.Twitter);
        }
        catch (Exception ex)
        {
            Log.Error(...);
        }

        SendNotificationOnComplete(count, ...); // Azure Service Bus / SignalR message back to app

        return count;
    }

FetchInstagramFetchFlickrFetchYouTube都遵循相同的逻辑。

1 个答案:

答案 0 :(得分:0)

我修改了我的代码,它似乎可以正常工作。

<强> AddPlace(...)

    public string AddPlace(Place placeBounds, ...)
    {

        FetchAllSocialPlatforms(placeBounds);

        Place newPlace = PlaceDB.AddPlace(placeBounds, ...);

        return new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(newPlace);
    }

<强> FetchAllSocialPlatforms(...)

    private async Task FetchAllSocialPlatforms(Place bounds)
    {
        try
        {
            Task taskTwitter = FetchTwitter(bounds);
            Task taskInstagram = FetchInstagram(bounds);
            Task taskFlickr = FetchFlickr(bounds);
            Task taskYouTube = FetchYouTube(bounds);

            var complete = Task.WhenAll(taskTwitter, taskInstagram, taskFlickr, taskYouTube);
            await complete.ContinueWith((c) =>
            {
                SendNotificationOnComplete(...); // Azure Service Bus / SignalR message back to app
            });

        }
        catch (Exception ex)
        {
            Log.Error(...);
        }

    }

<强> FetchTwitter(...)

    private async Task FetchTwitter(Place bounds)
    {
        var t = Task.Run(() =>
        {
            int count = 0;
            try
            {
                count = Fetch(bounds, PlatformType.Twitter);
            }
            catch (Exception ex)
            {
                Log.Error(...);
            }

            SendNotificationOnComplete(count, ...); // Azure Service Bus / SignalR message back to app

        });
        await t;
    }

同样,FetchInstagramFetchFlickrFetchYouTube都遵循类似的代码逻辑。