2017-03-17 37 views
0

我正在使用後臺任務獲取設備列表的.NET Core(使用完整的.net框架)中創建的網站上工作。 我想顯示一個加載「查看」像this而任務是從另一臺PC(使用GET請求)獲取數據,然後,當任務完成時,我想要顯示設備的表。我怎樣才能做到這一點?這裏是一小塊我的代碼:在.NET Core/Change視圖中創建加載視圖而不返回c#

public class DeviceController : Controller { 

     public IActionResult Index() { 

      if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Running) { 

       // TODO Show the loading screen here. 

       // return this.View("Loading"); 
      } 

      if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Faulted) { 
       ViewData["ErrorTitle"] = "Errore di sincronizzazione"; 
       ViewData["ErrorText"] = "Cannot get devices"; 
       return this.View("Error"); 
      } 

      if (DataSyncronizer.getDeviceListTask.Status == TaskStatus.Canceled) { 
       ViewData["ErrorTitle"] = "Errore di sincronizzazione"; 
       ViewData["ErrorText"] = ""; 
       return this.View("Error"); 
      } 

      return this.View(DataSyncronizer.Devices); 
     } 

這是獲取設備列表功能:

public static class DataSyncronizer { 

    public static Task<List<Device>> getDeviceListTask { get; private set; } 
    public static List<Device> Devices = new List<Device>(); 

    public static Task UpdateDevices() { 
         getDeviceListTask = new Task<List<Device>>(() => 
             Device.GetMyDevicesList(meUser)); 


         getDeviceListTask.ContinueWith((result) => { 
           DataSyncronizer.Devices = result.Result; 
          }, TaskScheduler.Current); 

         getDeviceListTask.Start(); 
         return getDeviceListTask; 
        } 
     } 

對不起,如果代碼是壞的,但我是新來的異步節目。

對不起,英語不好。

非常感謝。

回答

0

您可以在調用UpdateDevices()之前顯示加載器。

添加到您的任務結束

.ContinueWith(t => "Function to hide loader"); 

var webTask = Task.Run(() => 
{ 
    try 
    { 
     wcf.UploadMotionDynamicRaw(bytes); //my web service 
    } 
    catch (Exception ex) 
    { 
     //deal with error 
    } 
}).ContinueWith(t => "Function to hide loader"); 
+0

是的,我能做到這一點,但我不想因爲用戶可能會做別的事情而設備列表仍在加載中。我只想顯示加載視圖,如果用戶請求設備列表並且它尚未準備好。 – Edoardo396

+0

修改了可能有幫助的解決方案的帖子 –

+0

是的,但是如何在不返回IActionResult的情況下隱藏加載器(重定向到/ Devices/List)? – Edoardo396