private async void WatchingTask(string usage, Action <float> Update) { try { using (var performance = ProcessPerformanceCounterFactory.GetPerfCounterForProcess(process, usage, this.instanceName)) { while (this.refresh) { Update?.Invoke(performance.NextValue()); await Task.Delay(REFRESH_CYLCE); } } } catch (Exception) { // closing application } }
public void StartWatching() { try { if (string.IsNullOrEmpty(this.instanceName)) { this.instanceName = ProcessPerformanceCounterFactory.GetInstanceNameForProcess(process); } this.refresh = true; } catch (Exception) { // closing application } Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.PROCESSOR_USAGE, x => this.CPU = x); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.THREAD_COUNT, x => this.ThreadCount = x); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.MEMORY, x => this.Memory = x / KILO); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.VIRTUAL_MEMORY, x => this.VirtualMemory = x / KILO); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.PAGE_FILE, x => this.PageFile = x / KILO); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.PRIVATE_MEMORY, x => this.PrivateKBytes = x / KILO); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.READ_MEMORY, x => this.ReadKBytes = x / KILO); } ); Task.Run( () => { WatchingTask(ProcessPerformanceCounterFactory.WRITE_MEMORY, x => this.WriteKBytes = x / KILO); } ); }