.NET 内部效能插件

简介

这篇文章将介绍一个自制的简易插件。

因为在release版用windows的task manager感觉很麻烦,每次都要找一下自己的程序,就临时想到乾脆做一个简单的小插件来玩玩。

如何使用

static void Main()
{
    PerformanceDetector.Instance.initialize();
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new frmMain());
    PerformanceDetector.Instance.destroy();
} 

开启工程中的Program.cs,然后于Main中加入两行代码。

PerformanceDetector.Instance.initialize(); 

PerformanceDetector.Instance.destroy(); 


将第一行代码加于Application.Run()之前,将第二行代码加于Application.Run()之后。

就可以在运行时透过Alt+Ctrl+M呼叫这个插件了。

关于这插件的构想

因为不喜欢每次都用Task Manager,就自己整个了内存监控的代码。

一开始只是想说做个在应用内监控内存使用量的代码。所以使用Process来获取内存使用量。

但又觉得每次都去打开Log档来看很麻烦,就想说做个UI来看好了。

然后又想说,那要怎么显示这个控件?想了想既不想做在Menu也不想用一个按钮或其他控件,最后就决定用快捷来处理了。

不过第一版没有仔细想好,搞了个占据了全部画面的控件。用了才发现这样也不能边操作边看用量,好吧,重做。

所以最后决定了,就用一个单一的窗口来显示,会比较乾净。

想说CPU使用量也可以一起监控,就顺便做了。

本来是有在考虑原本第一版做的比较详细的需不需要有,但后来想想目前似乎还用不到,于是最后做成这样。

这工具中使用的技术

第一个关于CPU的使用部分,用的是PerformaceCounter来处理的

theCPUCounter = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
m_cpuUsage = (int)(theCPUCounter.NextValue() / Environment.ProcessorCount); 

public PerformanceCounter(
	string categoryName,
	string counterName,
	string instanceName )  


我需要抓的是当前进程的CPU使用率,所以第一个参数用"Process",第二个参数用"% Processor Time",第三个进程则是用当前进程的名称。

利用Process.GetCurrentProcess().ProcessName取得。

只要呼叫PerformanceCounter.NextValue()就可以取得CPU使用率,并透过Enviroment.ProcessorCount来计算。

另外由于我是利用快捷键的方式来显示窗口。所以利用了Hook来拦截按键。

HookManager.Instance.RegisterHook(HookType.WH_KEYBOARD, new CustomHookProc.HookProcHandler(KeyboardHookProc));

void KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    KeyStateInfo ctrlKey = KeyboardInfo.GetKeyState(Keys.ControlKey);
    KeyStateInfo altKey = KeyboardInfo.GetKeyState(Keys.Alt);
    KeyStateInfo mKey = KeyboardInfo.GetKeyState(Keys.M);
    if (ctrlKey.IsPressed && altKey.IsPressed && mKey.IsPressed)
    {
        showMonitorForm();
    }
} 

HookManager是我自己封装过的一个简易Hook类,只需透过注册事件即可使用。

最后是利用Process.WorkingSet64来取得进程内存。

Process currentProcess = Process.GetCurrentProcess();
currentProcess.WorkingSet64; 

以及使用Process.Modules跟ProcessModule.ModuleMemorySize来取得相关DLL的使用内存。


ProcessModuleCollection myProcessModuleCollection = currentProcess.Modules;
// Display the 'ModuleMemorySize' of each of the modules.
ProcessModule myProcessModule;
for (int i = 0; i < myProcessModuleCollection.Count; i++)
{
    myProcessModule = myProcessModuleCollection[i];
    if (m_moduleMemorys.Keys.Contains(myProcessModule.ModuleName))
        m_moduleMemorys[myProcessModule.ModuleName].update(myProcessModule.ModuleMemorySize);
    else
    {
        MemoryInfo mem = new MemoryInfo();
        mem.Name = myProcessModule.ModuleName;
        mem.update(myProcessModule.ModuleMemorySize);
        m_moduleMemorys[myProcessModule.ModuleName] = mem;
    }
} 


public class MemoryInfo
{
    string m_name;
    long m_currentUsage;
    long m_maximumUsage;
    public long MaximumUsage
    {
        get { return m_maximumUsage; }
        set { m_maximumUsage = value; }
    }
    public long CurrentUsage
    {
        get { return m_currentUsage; }
        set { m_currentUsage = value; }
    }
    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }
    public string GetCurrentKB()
    {
        return Convert.ToString(m_currentUsage / 1024.0);
    }
    public string GetMaximumKB()
    {
        return Convert.ToString(m_maximumUsage / 1024.0);
    }
    public void update(long current)
    {
        m_currentUsage = current;
        if (m_currentUsage > m_maximumUsage)
            m_maximumUsage = m_currentUsage;
    }
} 


在代码中,声明了一个简单的类,来储存信息。

Name: 储存 ProcessModule.ModuleName。

CurrentUsage: 储存现在内存用量。

MaximumUsage: 储存最大内存用量。

代码下载


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Chrome based Browser Engine for .NET EO.WebBrowser is a web browser engine based on Google's Chromium project but with native .NET programming interface --- don't worry, it's not a wrapper around the Chrome browser installed on your machine. In fact EO.WebBrowser has the whole browser engine embedded inside a single .NET DLL. In another word, it has zero external dependency. Just reference and use. Why use EO.WebBrowser? • Based on Google's Chrome Project EO.WebBrowser uses the same core Google's Chrome and Apple Safari uses. It does not rely on IE. The engine is much faster and safer. • Zero External Dependency What if user updates/uninstall their browser? What if user disables JavaScript in Internet Explorer's settings dialog? These questions does not exist with EO.WebBrowser because everything is embedded inside our DLL files. • Native .NET components written in C# Because it's written in .NET, you can use it with any .NET based language/development tool. The same DLL works for both 32 bit and 64 bit environments; • Easy to use Programming Interface EO.WebBrowser offers core components that can be used in any Windows application, as well as wrapper controls for both Windows Forms applications and WPF applications; • Extensive Customization Options EO.WebBrowser offers extensive customization options that allow you to customize context menu, hot keys, JavaScript dialogs, file dialogs, focus and window control. Together these features allow you to seamlessly integrate the browser engine into your application; • .NET code -> JavaScript code Turn any web page into an integral part of your application -- both visually and programmatically. You can execute JavaScript code and access all the JavaScript objects directly from your .NET code. Access their properties or even call a JavaScript function are all different options available to you; • JavaScript code -> .NET code Things always go both ways --- and this is reflected in our programming interface as well. You can call JavaScript code from .NET code, and the other way around is also true --- you can call .NET code from your JavaScript code. This allows your Web page to seamlessly interact with the host application; • Custom Resource Handler Want to keep an eye on everything? Or want to keep everything to yourself? We got you covered. EO.WebBrowser offers ability to intercept and modify all requests that originate from the browser engine. For example, you can automatically deny all request sent to a specific host. It also offers you the ability to implement custom protocols or custom resource handlers. For example, you can implement a custom request handler to load images from your database instead of a Web server;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值