2012-09-01 60 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

相关问题