-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from zhangjk95/master
Stop Etupirka from burning the CPU
- Loading branch information
Showing
3 changed files
with
180 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace Etupirka | ||
{ | ||
class ProcessInfo | ||
{ | ||
public int id { get; set; } | ||
|
||
public string processName { get; set; } | ||
|
||
public string path { get; set; } | ||
|
||
// True if we have access to this process. | ||
public bool accesible { get; set; } | ||
|
||
// True if the process info is pinned. Unpinned entries will be automatically cleaned up. | ||
public bool pinned { get; set; } | ||
} | ||
|
||
class ProcessInfoCleanup : IDisposable | ||
{ | ||
public ProcessInfoCleanup(ProcessInfoCache cache) | ||
{ | ||
this.cache = cache; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
cache.cleanUp(); | ||
} | ||
|
||
private ProcessInfoCache cache; | ||
} | ||
|
||
class ProcessInfoCache | ||
{ | ||
// Returns an object that automatically cleans up process info that is not used in the scope. | ||
public ProcessInfoCleanup scopedAccess() | ||
{ | ||
foreach (var entry in processInfoById) | ||
{ | ||
entry.Value.pinned = false; | ||
} | ||
|
||
return new ProcessInfoCleanup(this); | ||
} | ||
|
||
public void cleanUp() | ||
{ | ||
var entriesToRemove = processInfoById.Where((entry) => !entry.Value.pinned).ToList(); | ||
foreach (var entry in entriesToRemove) | ||
{ | ||
processInfoById.Remove(entry.Key); | ||
} | ||
} | ||
|
||
// Returns the path of the process, or an empty string if the process is not accesible. | ||
public string getProcessPath(Process p) | ||
{ | ||
if (processInfoById.ContainsKey(p.Id)) | ||
{ | ||
ProcessInfo info = processInfoById[p.Id]; | ||
if (info.processName == p.ProcessName) | ||
{ | ||
info.pinned = true; | ||
return info.path; | ||
} | ||
} | ||
|
||
var processInfo = new ProcessInfo | ||
{ | ||
id = p.Id, | ||
accesible = false, | ||
processName = p.ProcessName, | ||
pinned = true | ||
}; | ||
|
||
try | ||
{ | ||
processInfo.path = p.MainModule.FileName.ToLower(); | ||
processInfo.accesible = true; | ||
} | ||
catch { } | ||
|
||
processInfoById[p.Id] = processInfo; | ||
return processInfo.path; | ||
} | ||
|
||
private Dictionary<int, ProcessInfo> processInfoById = new Dictionary<int, ProcessInfo>(); | ||
} | ||
} |