Skip to content

Commit

Permalink
Fixed bug when performance watcher doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
L3tum committed Apr 2, 2018
1 parent 6ed3132 commit 23c7102
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions KCDModMerger/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,35 @@ private void InitializeUI()
private void InitializePerformanceWatchers()
{
Logger.Log("Initializing Performance Watcher");
cpuCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
ramCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
availableRamRounter = new PerformanceCounter("Memory", "Available Bytes");
diskCounter =
new PerformanceCounter("Process", "IO Data Bytes/sec", Process.GetCurrentProcess().ProcessName);
threadsCounter = new PerformanceCounter(".Net CLR LocksAndThreads", "# of current logical Threads",
Process.GetCurrentProcess().ProcessName);
cpuCounter.NextValue();
ramCounter.NextValue();
availableRamRounter.NextValue();
diskCounter.NextValue();
threadsCounter.NextValue();
timer = new Timer(UpdateUsages, null, 0, 1000);
try
{
cpuCounter = new PerformanceCounter("Process", "% Processor Time",
Process.GetCurrentProcess().ProcessName);
ramCounter = new PerformanceCounter("Process", "Working Set", Process.GetCurrentProcess().ProcessName);
availableRamRounter = new PerformanceCounter("Memory", "Available Bytes");
diskCounter =
new PerformanceCounter("Process", "IO Data Bytes/sec", Process.GetCurrentProcess().ProcessName);
threadsCounter = new PerformanceCounter(".Net CLR LocksAndThreads", "# of current logical Threads",
Process.GetCurrentProcess().ProcessName);
cpuCounter.NextValue();
ramCounter.NextValue();
availableRamRounter.NextValue();
diskCounter.NextValue();
threadsCounter.NextValue();
timer = new Timer(UpdateUsages, null, 0, 1000);
}
catch (InvalidOperationException e)
{
Logger.Log("Performance Watchers: " + e.Message);
}
finally
{
cpuCounter = null;
ramCounter = null;
availableRamRounter = null;
diskCounter = null;
threadsCounter = null;
}
Logger.Log("Initialized Performance Watcher!");
}

Expand Down Expand Up @@ -307,7 +323,7 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
/// <param name="state">The state.</param>
private void UpdateUsages(object state)
{
if (isInformationVisible)
if (isInformationVisible && cpuCounter != null && ramCounter != null && availableRamRounter != null && diskCounter != null && threadsCounter != null)
{
var cpu = Math.Round(cpuCounter.NextValue()) + "%";
var ram = App.ConvertToHighest((long) ramCounter.NextValue()) + " (" +
Expand Down
Binary file modified KCDModMerger/bin/Release/KCDModMerger.exe
Binary file not shown.
Binary file modified KCDModMerger/bin/Release/KCDModMerger.pdb
Binary file not shown.
Binary file modified KCDModMerger/bin/Release/KCDModMerger.zip
Binary file not shown.

0 comments on commit 23c7102

Please sign in to comment.