2012-09-01 95 views
0

我開發了一個Web API用於機器上的性能監視,當通過URL調用資源時,信息以json格式打印在瀏覽器中。其中一種方法有一個循環,每秒都會獲得一個新的數據值。如何在返回數據後繼續執行方法?

是否有可能在每次迭代時「返回」值並將其顯示在瀏覽器的json中,從而在每次迭代時自動更新?換句話說,我試圖創建一個實時更新機制,而不是等待程序執行,然後在性能數據日誌完成後顯示json。

public List<LogInfo> LogTimedPerfData(string macName, string categoryName, string counterName, 
              string instanceName, string logName, long? seconds) 
    { 
     iModsDBRepository modsDB = new iModsDBRepository(); 
     List<MachineInfo> theMac = modsDB.GetMachineByName(macName); 

     if (theMac.Count == 0) 
      return new List<LogInfo>(); 

     else if (instanceName == null) 
     { 
      if (!PerformanceCounterCategory.Exists(categoryName, macName) || 
       !PerformanceCounterCategory.CounterExists(counterName, categoryName, macName)) 
      { 
       return new List<LogInfo>(); 
      } 
     } 
     else if (instanceName != null) 
     { 
      if (!PerformanceCounterCategory.Exists(categoryName, macName) || 
       !PerformanceCounterCategory.CounterExists(counterName, categoryName, macName) || 
       !PerformanceCounterCategory.InstanceExists(instanceName, categoryName, macName)) 
      { 
       return new List<LogInfo>(); 
      } 
     } 
     else if (logName == null) 
     { 
      return new List<LogInfo>(); 
     } 

     // Check if entered log name is a duplicate for the authenticated user 
     List<LogInfo> checkDuplicateLog = this.GetSingleLog(logName); 
     if (checkDuplicateLog.Count > 0) 
     { 
      return new List<LogInfo>(); 
     } 

     PerformanceCounterCategory category = new PerformanceCounterCategory(categoryName, theMac[0].MachineName); 
     if (category.CategoryName == null || category.MachineName == null) 
     { 
      return new List<LogInfo>(); 
     } 

     List<LogInfo> logIt = new List<LogInfo>(); 
     if (category.CategoryType != PerformanceCounterCategoryType.SingleInstance) 
     { 
      List<InstanceInfo> instances = modsDB.GetInstancesFromCatMacName(theMac[0].MachineName, category.CategoryName); 

      foreach (InstanceInfo inst in instances) 
      { 
       if (!category.InstanceExists(inst.InstanceName)) 
       { 
        continue; 
       } 
       else if (inst.InstanceName.Equals(instanceName, StringComparison.OrdinalIgnoreCase)) 
       { 
        PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName, 
                     inst.InstanceName, theMac[0].MachineName); 

        string data = ""; 
        List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name); 

        string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt"); 

        string[] dataValues = new string[(int)seconds]; 
        for (int i = 0; i < seconds; i++) 
        { 
         data = "Value " + i + ": " + perfCounter.NextValue().ToString(); 
         dataValues[i] = data; 
         Thread.Sleep(1000); 
        } 
        string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt"); 

        Log log = new Log 
        { 
         LogName = logName, 
         CounterName = perfCounter.CounterName, 
         InstanceName = perfCounter.InstanceName, 
         CategoryName = perfCounter.CategoryName, 
         MachineName = perfCounter.MachineName, 
         TimeStarted = timeStarted, 
         TimeFinished = timeFinished, 
         PerformanceData = string.Join(",", dataValues), 
         UserID = currUser[0].UserID 
        }; 
        this.CreateLog(log); 
        logIt.Add(new LogInfo 
        { 
         LogName = logName, 
         CounterName = perfCounter.CounterName, 
         InstanceName = perfCounter.InstanceName, 
         CategoryName = perfCounter.CategoryName, 
         MachineName = perfCounter.MachineName, 
         TimeStarted = timeStarted, 
         TimeFinished = timeFinished, 
         PerformanceData = dataValues.ToList<string>() 
        }); 
        break; 
       } 
      } 
     } 
     else 
     { 
      PerformanceCounter perfCounter = new PerformanceCounter(categoryName, counterName, 
                     "", theMac[0].MachineName); 


      string data = ""; 
      List<UserInfo> currUser = this.GetUserByName(WindowsIdentity.GetCurrent().Name); 

      string timeStarted = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt"); 

      string[] dataValues = new string[(int)seconds]; 
      for (int i = 0; i < seconds; i++) 
      { 
       data = "Value " + i + ": " + perfCounter.NextValue().ToString(); 
       dataValues[i] = data; 
       Thread.Sleep(1000); 
      } 
      string timeFinished = DateTime.Now.ToString("MM/dd/yyyy - h:mm:ss tt"); 

      Log log = new Log 
      { 
       LogName = logName, 
       CounterName = perfCounter.CounterName, 
       InstanceName = perfCounter.InstanceName, 
       CategoryName = perfCounter.CategoryName, 
       MachineName = perfCounter.MachineName, 
       TimeStarted = timeStarted, 
       TimeFinished = timeFinished, 
       PerformanceData = string.Join(",", dataValues), 
       UserID = currUser[0].UserID 
      };    
      this.CreateLog(log); 
      logIt.Add(new LogInfo 
      { 
       LogName = logName, 
       CounterName = perfCounter.CounterName, 
       InstanceName = perfCounter.InstanceName, 
       CategoryName = perfCounter.CategoryName, 
       MachineName = perfCounter.MachineName, 
       TimeStarted = timeStarted, 
       TimeFinished = timeFinished, 
       PerformanceData = dataValues.ToList<string>() 
      }); 
     } 
     return logIt; 
    } 
+0

它會如何幫助我的原因是什麼?是的,我可以在asnwer幫助解決問題時點擊對號。問題在於我的大部分問題,不幸的是並非如此。 – praetor

回答

1

你可能想要做SignalR一些研究,有一對夫婦的教程有上推通知瀏覽器與SignalR。

的可能有用的鏈接一把:

+0

非常感謝。看起來它可以完成工作。這些例子似乎可以實現它的觀點。我不確定它是否適用於通過URL調用API返回的json,但我會給它一個! – praetor

相關問題