我为随机时间Random执行Action(void委托)的类创建了一个扩展方法:
public static class RandomExtension
{
private static bool _isAlive;
private static Task _executer;
public static void ExecuteRandomAsync(this Random random, int min, int max, int minDuration, Action action)
{
Task outerTask = Task.Factory.StartNew(() =>
{
_isAlive = true;
_executer = Task.Factory.StartNew(() => { ExecuteRandom(min, max, action); });
Thread.Sleep(minDuration);
StopExecuter();
});
}
private static void StopExecuter()
{
_isAlive = false;
_executer.Wait();
_executer.Dispose();
_executer = null;
}
private static void ExecuteRandom(int min, int max, Action action)
{
Random …Run Code Online (Sandbox Code Playgroud) 我正在连接到该Process类别中的各种性能计数器.我使用以下c#方法来确定获取计数器时要使用的实例名称:
private const string _categoryName = "Process";
private const string _processIdCounter = "ID Process";
public static bool TryGetInstanceName(Process process, out string instanceName)
{
PerformanceCounterCategory processCategory = new PerformanceCounterCategory(_categoryName);
string[] instanceNames = processCategory.GetInstanceNames();
foreach (string name in instanceNames)
{
using (PerformanceCounter processIdCounter = new PerformanceCounter(_categoryName, _processIdCounter, name, true))
{
if (process.Id == (int)processIdCounter.RawValue)
{
instanceName = name;
return true;
}
}
}
instanceName = null;
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在,我注意到返回的实例名称通常与值相匹配Process.ProcessName.
实例名称和进程名称如何相关?
我问,因为我想简化foreach例程中的循环,这样我就不必获取ID Process …