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
StringBuilder stringBuilder2 = new StringBuilder(1024); | |
if (Microsoft.Win32.NativeMethods.GetModuleFileNameEx( | |
safeProcessHandle, | |
new HandleRef(null, handle), | |
stringBuilder2, | |
stringBuilder2.Capacity * 2 | |
) == 0) |
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
[DllImport("psapi.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] | |
private static extern int GetModuleFileNameEx(SafeProcessHandle processHandle, IntPtr moduleHandle, StringBuilder baseName, int size); |
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
private static Func<Process, string> GetProcessNameAccessor() | |
{ | |
var param = Expression.Parameter(typeof(Process), "arg"); | |
var processInfoMember = Expression.Field(param, "processInfo"); | |
var processNameMember = Expression.Field(processInfoMember, "processName"); | |
var lambda = Expression.Lambda(typeof(Func<Process, string>), processNameMember, param); | |
return (Func<Process, string>)lambda.Compile(); | |
} |
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
private static Type _processInfoType = null; | |
private static FieldInfo _processNameField = null; | |
public static string GetProcessNameByReflection(Process p) | |
{ | |
var processInfoField = typeof(System.Diagnostics.Process) | |
.GetField("processInfo", BindingFlags.Instance | BindingFlags.NonPublic); | |
var processInfo = processInfoField.GetValue(p); | |
if (_processInfoType == null) | |
{ |
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
private readonly StringBuilder _baseNameBuilder = new StringBuilder(1024); | |
public static string GetProcessNameNative(Process p) | |
{ | |
_baseNameBuilder.Clear(); | |
if (GetModuleFileNameEx(p.SafeHandle, IntPtr.Zero, _baseNameBuilder, _baseNameBuilder.Capacity) == 0) | |
{ | |
_baseNameBuilder.Append("???"); | |
} | |
return _baseNameBuilder.ToString(); |
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
public static bool IsProcessRunning(int processId) | |
{ | |
return IsProcessRunning(processId, GetProcessIds()); | |
} |
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
public static SafeProcessHandle OpenProcess(int processId, int access, bool throwIfExited) | |
{ | |
SafeProcessHandle safeProcessHandle = NativeMethods.OpenProcess(access, false, processId); | |
int lastWin32Error = Marshal.GetLastWin32Error(); | |
if (!safeProcessHandle.IsInvalid) | |
return safeProcessHandle; | |
// error handling | |
if (processId == 0) | |
throw new Win32Exception(5); |
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
_workers = new Task[workerCount]; | |
for (int i = 0; i < workerCount; i++) | |
{ | |
_workers[i] = Task.Run(async () => | |
{ | |
while (true) | |
{ | |
lock (_lock) | |
{ | |
Thread.Sleep(5); |
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
private void OnContentionStop(ContentionTraceData data) | |
{ | |
ContentionInfo info = _contentionStore.GetContentionInfo(data.ProcessID, data.ThreadID); | |
if (info == null) | |
return; | |
// unlucky case when we start to listen just after the ContentionStart event | |
if (info.ContentionStartRelativeMSec == 0) | |
return; |
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
private void OnContentionStart(ContentionTraceData data) | |
{ | |
ContentionInfo info = _contentionStore.GetContentionInfo(data.ProcessID, data.ThreadID); | |
if (info == null) | |
return; | |
info.TimeStamp = data.TimeStamp; | |
info.ContentionStartRelativeMSec = data.TimeStampRelativeMSec; | |
} |
NewerOlder