相关疑难解决方法(0)

使用Thread.Sleep()总是坏的吗?

我为随机时间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)

c# multithreading sleep

35
推荐指数
3
解决办法
4864
查看次数

性能计数器实例名称与进程名称

我正在连接到该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 …

c# process performancecounter

4
推荐指数
1
解决办法
3847
查看次数

标签 统计

c# ×2

multithreading ×1

performancecounter ×1

process ×1

sleep ×1