2016-05-16 4 views
0

MVC C#Razor Framework 4.6で作業しています。タスク実行(非同期)の応答で部分ビューを使用してUIを更新します。

長時間実行レポートでは、がTask.Runにラップされています。このメソッドはbooleanの値を返し、それに基づいてパーシャルビュー(_NotificationPanel)をリフレッシュしています。

public ActionResult ExportCannedReport(string cannedReportKey, string cannedReportName) 
{    
    string memberKeys = _curUser.SecurityInfo.AccessibleFacilities_MemberKeys; //ToDo: Make sure this is fine or need to pass just self member? 
    string memberIds = _curUser.SecurityInfo.AccessibleFacilities_MemberIDs; //ToDo: Make sure this is fine or need to pass just self member? 
    string curMemberNameFormatted = _curUser.FacilityInfo.FacilityName.Replace(" ", string.Empty); 
    string cannedReportNameFormatted = cannedReportName.Replace(" ", string.Empty); 
    string fileName = string.Concat(cannedReportNameFormatted, "_", DateTime.Now.ToString("yyyyMMdd"), "_", curMemberNameFormatted); 
    //ToDo: Make sure below getting userId is correct 
    string userId = ((_curUser.IsECRIStaff.HasValue && _curUser.IsECRIStaff.Value) ? _curUser.MembersiteUsername : _curUser.PGUserName); 

    var returnTask = Task.Run<bool>(() => ExportManager.ExportExcelCannedReportPPR(cannedReportKey, cannedReportName, fileName, memberIds, userId)); 
    returnTask.ContinueWith((antecedent) => 
    { 
     if (antecedent.Result == true) 
     { 
      return PartialView("_NotificationPanel", "New file(s) added in 'Download Manager'."); 
     } 
     else 
     { 
      return PartialView("_NotificationPanel", "An error occurred while generating the report."); 
     } 
    }, TaskContinuationOptions.OnlyOnRanToCompletion); 

    return PartialView("_NotificationPanel", ""); 
} 

今の問題は、UIがContinueWith_NotificationPanelを実行しますにもかかわらず、リフレッシュすることができませんでしたということです。

+0

本当にそのコードで何が起こると思いますか? –

答えて

1

問題は、一度それから戻ってくると、その要求が完了したことです。 1回のリクエストで複数回返すことはできません。要求と応答は1対1です。 asyncawaitをここで使用する必要があります。その場合、エクスポートが完了してから結果が返されるだけです。

public async Task<ActionResult> ExportCannedReport(string cannedReportKey, 
                string cannedReportName) 
{    
    // Omitted for brevity... 

    var result = 
     await Task.Run<bool>(() => 
      ExportManager.ExportExcelCannedReportPPR(cannedReportKey, 
                cannedReportName, 
                fileName, 
                memberIds, 
                userId)); 

    return PartialView("_NotificationPanel", 
     result 
      ? "New file(s) added in 'Download Manager'." 
      : "An error occurred while generating the report."); 
} 

は、あなたはそれが「awaitable」であるように、返すメソッドTaskを作成する必要があります。次に、awaitキーワードを有効にする方法をasyncとマークします。最後に、長時間実行されるタスクを実行する準備が整いました。結果から、必要な部分ビューの更新を正しく決定して返します。また

更新

サーバーが応答したら、クライアントおよび更新にAJAXコールを利用することができます。具体的なチェックアウトの詳細については、MSDNをご覧ください。

+0

Davidに感謝します。実際には、ユーザーは他の機能にアクセスすることができます。 Task.Runの実行が終了すると、ユーザーは他の機能にアクセスできます。最後に同期メソッドになります。あなたはあなたの提案を提供してもらえますか? –

関連する問題