相关疑难解决方法(0)

如何在C#中获得CPU使用率?

我想获得C#中应用程序的总CPU使用率.我已经找到了许多方法来深入研究进程的属性,但我只想要进程的CPU使用率,以及你在TaskManager中获得的总CPU.

我怎么做?

c# cpu-usage

191
推荐指数
6
解决办法
26万
查看次数

在ASP.NET中正确实现后台进程Thread

我需要执行一个无限的while循环,并希望启动执行global.asax.我的问题是我该怎么做?我应该开始新线程还是应该使用Async和Task或其他任何东西?在while循环中,我需要做 await TaskEx.Delay(5000);

我该怎么做才能阻止任何其他进程,不会造成内存泄漏?

我使用VS10,AsyncCTP3,MVC4

编辑:

 public void SignalRConnectionRecovery()
        {
            while (true)
            {
                Clients.SetConnectionTimeStamp(DateTime.UtcNow.ToString());
                await TaskEx.Delay(5000);
            }
        }
Run Code Online (Sandbox Code Playgroud)

我需要做的就是在应用程序可用时将其作为全局单例实例运行.

编辑:解决

这是Global.asax的最终解决方案

protected void Application_Start()
{
    Thread signalRConnectionRecovery = new Thread(SignalRConnectionRecovery);
    signalRConnectionRecovery.IsBackground = true;
    signalRConnectionRecovery.Start();

    Application["SignalRConnectionRecovery"] = signalRConnectionRecovery;
}


protected void Application_End()
{
    try
    {
        Thread signalRConnectionRecovery = (Thread)Application["SignalRConnectionRecovery"];
        if (signalRConnectionRecovery != null && signalRConnectionRecovery.IsAlive)
        {
            signalRConnectionRecovery.Abort();
        }
    }
    catch
    {
            ///
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现这篇关于如何使用异步工作者的好文章:http: //www.dotnetfunda.com/articles/article613-background-processes-in-asp-net-web-applications.aspx

这个:http: //code.msdn.microsoft.com/CSASPNETBackgroundWorker-dda8d7b6

但我觉得我的需求会很完美:http: //forums.asp.net/t/1433665.aspx/1

asp.net asp.net-mvc multithreading

15
推荐指数
4
解决办法
3万
查看次数

如何在不让用户在命令提示符下使用lodctr重建的情况下使性能计数器工作?

我一直试图在C#中获得Windows PC(Windows 7运行.Net 4.5)的总CPU使用率.看起来使用PerformanceCounter应该能够满足我的需求.

我根据以下三个链接编写了一些试用代码(并检查了msdn页面),这是最基本的版本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace EntropyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformanceCounter cpuCounter;

            cpuCounter = new PerformanceCounter();

            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";


            while(true)
            {
                try
                {
                    float firstValue = cpuCounter.NextValue();
                    System.Threading.Thread.Sleep(500);
                    Console.WriteLine("Before getting processor:");
                    float currentCpuUsage = cpuCounter.NextValue();
                    Console.WriteLine("After getting processor:");
                    System.Threading.Thread.Sleep(1000); 

                    Console.WriteLine(currentCpuUsage);
                }
                catch (Exception e)
                {
                    Console.WriteLine("\n{0}\n", e.Message);
                }
                System.Threading.Thread.Sleep(10000);

            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

每当调用NextValue时,都会触发下面的异常错误.这似乎是性能计数器值出现问题的常见问题. …

c# performancecounter cpu-usage

7
推荐指数
1
解决办法
1321
查看次数