public void StatsUpdaterWorker() { var usages = new CpuRamUsage(); var cpuUsage = new PerformanceCounter("Processor", "% Processor Time", "_Total"); var TotalRam = new PerformanceCounter("Memory", "Available MBytes"); while (true) { // Update Cpu/Ram Usage usages.Update(); double diabloCpuUsage = 0; long diabloRamUsage = 0; int diabloCount = 0; double demonbuddyCpuUsage = 0; long demonbuddyRamUsage = 0; int demonbuddyCount = 0; foreach (var bot in BotSettings.Instance.Bots) { // Update bot uptime bot.RunningTime = bot.IsRunning ? DateTime.Now.Subtract(bot.StartTime).ToString(@"hh\hmm\mss\s") : ""; // Calculate total Cpu/Ram usage for Diablo if (bot.Diablo.IsRunning) { try { var usage = usages.GetUsageById(bot.Diablo.Proc.Id); diabloCpuUsage += usage.Cpu; diabloRamUsage += usage.Memory; diabloCount++; } catch {} } // Calculate total Cpu/Ram usage for Demonbuddy if (bot.Demonbuddy.IsRunning) { try { var usage = usages.GetUsageById(bot.Demonbuddy.Proc.Id); demonbuddyCpuUsage += usage.Cpu; demonbuddyRamUsage += usage.Memory; demonbuddyCount++; } catch {} } } // Update Stats label on mainform updateMainformStatsLabel( string.Format("Cpu Usage : Diablo[{0}] {1}%, Demonbuddy[{2}] {3}%, Total {4}%" + Environment.NewLine + "Ram Usage : Diablo[{0}] {5}Gb, Demonbuddy[{2}] {6}Mb, Total {7}Gb", diabloCount, Math.Round(diabloCpuUsage, 1), demonbuddyCount, Math.Round(demonbuddyCpuUsage, 1), Math.Round(diabloCpuUsage + demonbuddyCpuUsage, 1), Math.Round(diabloRamUsage / Math.Pow(2, 30), 2), Math.Round(demonbuddyRamUsage / Math.Pow(2, 20), 2), Math.Round((diabloRamUsage / Math.Pow(2, 30)) + (demonbuddyRamUsage / Math.Pow(2, 30)), 2))); Thread.Sleep(500); } }
public void StatsUpdaterWorker() { // Wait here till mainform is up while (Program.Mainform == null) { Thread.Sleep(100); } var usages = new CpuRamUsage(); var totalRam = PerformanceInfo.GetTotalMemory(); prepareMainGraphCpu(); prepareMainGraphMemory(); prepareMainGraphConnections(); prepareMainGraphGold(); while (true) { // Update Cpu/Ram Usage usages.Update(); double diabloCpuUsage = 0; long diabloRamUsage = 0; int diabloCount = 0; double demonbuddyCpuUsage = 0; long demonbuddyRamUsage = 0; int demonbuddyCount = 0; double goldPerHour = 0; double totalGold = 0; foreach (var bot in BotSettings.Instance.Bots) { var chartStats = bot.ChartStats; // Update bot uptime bot.RunningTime = bot.IsRunning ? DateTime.Now.Subtract(bot.StartTime).ToString(@"hh\hmm\mss\s") : ""; // Update bot specific Chart stats CreateChartStats(bot, Program.Mainform.GoldStats, ChartValueType.Double); if (bot.IsRunning) { #region Calculate System Usage if (bot.Diablo.IsRunning) { // Calculate total Cpu/Ram usage for Diablo try { var usage = usages.GetUsageById(bot.Diablo.Proc.Id); diabloCpuUsage += usage.Cpu; diabloRamUsage += usage.Memory; diabloCount++; } catch { } } if (bot.Demonbuddy.IsRunning) { // Calculate total Cpu/Ram usage for Demonbuddy try { var usage = usages.GetUsageById(bot.Demonbuddy.Proc.Id); demonbuddyCpuUsage += usage.Cpu; demonbuddyRamUsage += usage.Memory; demonbuddyCount++; } catch { } } #endregion #region Gold Stats chartStats.GoldStats.Update(bot); // Update Current bot // Calculate total gold for all bots goldPerHour += chartStats.GoldStats.GoldPerHour; totalGold += chartStats.GoldStats.LastCoinage; var serie = Program.Mainform.GoldStats.Series.FirstOrDefault(x => x.Name == bot.Name); if (serie != null) { updateMainformGraph(Program.Mainform.GoldStats, serie.Name, Math.Round(chartStats.GoldStats.GoldPerHour), limit: (int)Properties.Settings.Default.StatsGphHistory, autoscale: true); } #endregion } else { } } try { // add to Cpu graph var graph = Program.Mainform.CpuUsage; var allusage = diabloCpuUsage + demonbuddyCpuUsage; updateMainformGraph(graph, "All Usage", allusage, legend: string.Format("All usage: {0,11}%", allusage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsCPUHistory); updateMainformGraph(graph, "Diablo", diabloCpuUsage, legend: string.Format("Diablo: {0,16}%", diabloCpuUsage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsCPUHistory); updateMainformGraph(graph, "Demonbuddy", demonbuddyCpuUsage, legend: string.Format("Demonbuddy: {0,4}%", demonbuddyCpuUsage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsCPUHistory); updateMainformGraph(graph, "Total System", Math.Round(usages.TotalCpuUsage, 2), legend: string.Format("Total System: {0,2}%", usages.TotalCpuUsage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsCPUHistory); // add to Memory graph graph = Program.Mainform.MemoryUsage; allusage = (double)(diabloRamUsage + demonbuddyRamUsage) / totalRam * 100; var diablousage = (double)diabloRamUsage / totalRam * 100; var demonbuddyusage = (double)demonbuddyRamUsage / totalRam * 100; updateMainformGraph(graph, "All Usage", allusage, legend: string.Format("All usage: {0,11}%", ((double)(diabloRamUsage + demonbuddyRamUsage) / totalRam * 100).ToString("000.0")), limit: (int)Properties.Settings.Default.StatsMemoryHistory); updateMainformGraph(graph, "Diablo", diablousage, legend: string.Format("Diablo: {0,16}%", diablousage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsMemoryHistory); updateMainformGraph(graph, "Demonbuddy", demonbuddyusage, legend: string.Format("Demonbuddy: {0,4}%", demonbuddyusage.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsMemoryHistory); var mem = (double)PerformanceInfo.GetPhysicalUsedMemory() / totalRam * 100; updateMainformGraph(graph, "Total System", mem, legend: string.Format("Total System: {0,2}%", mem.ToString("000.0")), limit: (int)Properties.Settings.Default.StatsMemoryHistory); // add to Connection graph updateMainformGraph(Program.Mainform.CommConnections, "Connections", Communicator.StatConnections, legend: string.Format("Connections {0}", Communicator.StatConnections), autoscale: true, limit: (int)Properties.Settings.Default.StatsConnectionsHistory); updateMainformGraph(Program.Mainform.CommConnections, "Failed", Communicator.StatFailed, legend: string.Format("Failed {0}", Communicator.StatFailed), autoscale: true, limit: (int)Properties.Settings.Default.StatsConnectionsHistory); Communicator.StatConnections = 0; Communicator.StatFailed = 0; // add to Gold Graph updateMainformGraph(Program.Mainform.GoldStats, "Gph", Math.Round(goldPerHour), legend: string.Format("Gph {0}", Math.Round(goldPerHour)), autoscale: true, limit: (int)Properties.Settings.Default.StatsGphHistory); updateMainformLabel(Program.Mainform.CashPerHour, string.Format("{0:C2}", (goldPerHour / 1000000 * (double)Properties.Settings.Default.StatsGoldPrice))); updateMainformLabel(Program.Mainform.CurrentCash, string.Format("{0:C2}", (totalGold / 1000000 * (double)Properties.Settings.Default.StatsGoldPrice))); updateMainformLabel(Program.Mainform.TotalGold, string.Format("{0:N0}", totalGold)); } catch (Exception ex) { DebugHelper.Exception(ex); } Thread.Sleep((int)Properties.Settings.Default.StatsUpdateRate); } }
public void StatsUpdaterWorker() { // Wait here till mainform is up while (Program.Mainform == null || !Program.Mainform.IsHandleCreated) { Thread.Sleep(100); } var usages = new CpuRamUsage(); var totalRam = PerformanceInfo.GetTotalMemory(); PrepareMainGraphCpu(); PrepareMainGraphMemory(); PrepareMainGraphGold(); while (true) { // Update Cpu/Ram Usage usages.Update(); double diabloCpuUsage = 0; long diabloRamUsage = 0; double demonbuddyCpuUsage = 0; long demonbuddyRamUsage = 0; double goldPerHour = 0; double totalGold = 0; lock (BotSettings.Instance) { foreach (var bot in BotSettings.Instance.Bots.ToList()) { var chartStats = bot.ChartStats; // Update bot uptime bot.RunningTime = bot.IsRunning ? DateTime.UtcNow.Subtract(bot.StartTime).ToString(@"hh\hmm\mss\s") : ""; // Update bot specific Chart stats CreateChartStats(bot, Program.Mainform.GoldStats, ChartValueType.Double); if (bot.IsRunning) { #region Calculate System Usage if (bot.Diablo.IsRunning) { // Calculate total Cpu/Ram usage for Diablo try { var usage = usages.GetUsageById(bot.Diablo.Proc.Id); diabloCpuUsage += usage.Cpu; diabloRamUsage += usage.Memory; } catch (Exception) { } } if (bot.Demonbuddy.IsRunning) { // Calculate total Cpu/Ram usage for Demonbuddy try { var usage = usages.GetUsageById(bot.Demonbuddy.Proc.Id); demonbuddyCpuUsage += usage.Cpu; demonbuddyRamUsage += usage.Memory; } catch (Exception) { } } #endregion #region Gold Stats chartStats.GoldStats.Update(bot); // Update Current bot // Calculate total gold for all bots goldPerHour += chartStats.GoldStats.GoldPerHour; totalGold += chartStats.GoldStats.LastCoinage; var serie = Program.Mainform.GoldStats.Series.FirstOrDefault(x => x.Name == bot.Name); if (serie != null) { UpdateMainformGraph(Program.Mainform.GoldStats, serie.Name, Math.Round(chartStats.GoldStats.GoldPerHour), (int)Settings.Default.StatsGphHistory, autoscale: true); } #endregion } } } try { // add to Cpu graph var graph = Program.Mainform.CpuUsage; var allusage = diabloCpuUsage + demonbuddyCpuUsage; UpdateMainformGraph(graph, "All Usage", allusage, legend: $"All usage: {allusage,11:000.0}%", limit: (int)Settings.Default.StatsCPUHistory); UpdateMainformGraph(graph, "Diablo", diabloCpuUsage, legend: $"Diablo: {diabloCpuUsage,16:000.0}%", limit: (int)Settings.Default.StatsCPUHistory); UpdateMainformGraph(graph, "Demonbuddy", demonbuddyCpuUsage, legend: $"Demonbuddy: {demonbuddyCpuUsage,4:000.0}%", limit: (int)Settings.Default.StatsCPUHistory); UpdateMainformGraph(graph, "Total System", Math.Round(usages.TotalCpuUsage, 2), legend: $"Total System: {usages.TotalCpuUsage,2:000.0}%", limit: (int)Settings.Default.StatsCPUHistory); // add to Memory graph graph = Program.Mainform.MemoryUsage; allusage = (double)(diabloRamUsage + demonbuddyRamUsage) / totalRam * 100; var diablousage = (double)diabloRamUsage / totalRam * 100; var demonbuddyusage = (double)demonbuddyRamUsage / totalRam * 100; UpdateMainformGraph(graph, "All Usage", allusage, legend: $"All usage: {((double)(diabloRamUsage + demonbuddyRamUsage) / totalRam * 100),11:000.0}%", limit: (int)Settings.Default.StatsMemoryHistory); UpdateMainformGraph(graph, "Diablo", diablousage, legend: $"Diablo: {diablousage,16:000.0}%", limit: (int)Settings.Default.StatsMemoryHistory); UpdateMainformGraph(graph, "Demonbuddy", demonbuddyusage, legend: $"Demonbuddy: {demonbuddyusage,4:000.0}%", limit: (int)Settings.Default.StatsMemoryHistory); var mem = (double)PerformanceInfo.GetPhysicalUsedMemory() / totalRam * 100; UpdateMainformGraph(graph, "Total System", mem, legend: $"Total System: {mem,2:000.0}%", limit: (int)Settings.Default.StatsMemoryHistory); // add to Gold Graph UpdateMainformGraph(Program.Mainform.GoldStats, "Gph", Math.Round(goldPerHour), legend: $"Gph {Math.Round(goldPerHour)}", autoscale: true, limit: (int)Settings.Default.StatsGphHistory); UpdateMainformLabel(Program.Mainform.CashPerHour, $"{(goldPerHour / 1000000 * (double)Settings.Default.StatsGoldPrice):C2}"); UpdateMainformLabel(Program.Mainform.CurrentCash, $"{(totalGold / 1000000 * (double)Settings.Default.StatsGoldPrice):C2}"); UpdateMainformLabel(Program.Mainform.TotalGold, $"{totalGold:N0}"); } catch (Exception ex) { DebugHelper.Exception(ex); } Thread.Sleep((int)Settings.Default.StatsUpdateRate); } }