前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 操作线程的通用类[测试通过]

C# 操作线程的通用类[测试通过]

作者头像
磊哥
发布2018-04-26 16:44:22
7940
发布2018-04-26 16:44:22
举报
文章被收录于专栏:王磊的博客王磊的博客

进程管理就是对服务器性能的管理和协调,在程序的运行角度来看非常重要,也可以根据操作进程的手段,衍生很多实用和智能的功能,以下就是介绍一个自己写的进程通用操作类,功能如下:

1.把ProcessUtility类直接复制到程序中即可实用。

2.调用方法 ex:ProcessUtility.resetProcessByPName("explorer");

代码如下:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

/// <summary>
/// author:Stone_W
/// date:2010.12.20
/// desc:进程管理类
/// </summary>
public class ProcessUtility
{
    #region 进程的cpu使用率
    /// <summary>
    /// 进程的cpu使用率
    /// </summary>
    /// <param name="processName">进程的名称</param>
    /// <returns>string</returns>
    public static string GetProcessRate(string processName)
    {
        string result = String.Empty;
        try
        {
            PerformanceCounter pfc = new PerformanceCounter();      // 性能计数器
            pfc.CategoryName = "Process";                           // 指定获取计算机进程信息
            pfc.CounterName = "% Processor Time";                   // 占有率
            pfc.InstanceName = processName;                         // 指定进程   
            pfc.MachineName = ".";
            result = Math.Round(pfc.NextValue(), 2) + "%";
        }
        catch (Exception ex) { }
        return result;
    }
    #endregion

    #region 进程内存使用(单位:K)
    /// <summary>
    /// 进程内存使用(单位:K)
    /// </summary>
    /// <param name="pcs">进程实体</param>
    /// <returns>string</returns>
    public static string GetProcessDDR(Process pcs)
    {
        string result = String.Empty;
        try
        {
            result = (pcs.PrivateMemorySize / 1000.0).ToString();
        }
        catch (Exception ex) { }
        return result;
    }
    #endregion

    #region 关闭进程
    /// <summary>
    /// 关闭进程
    /// </summary>
    /// <param name="pName">进程的名称</param>
    /// <returns>bool</returns>
    public static bool StopProcessByPName(string pName)
    {
        bool result = false;
        if (!String.IsNullOrEmpty(pName))
        {
            try
            {
                Process[] myProcesses = Process.GetProcesses();
                foreach (System.Diagnostics.Process myProcess in myProcesses)
                {
                    if (myProcess.ProcessName.ToUpper() == pName.ToUpper().Trim())
                    {
                        myProcess.Kill();
                    }
                }
                result = true;
            }
            catch (Exception ex) { }
        }
        return result;

    }
    #endregion

    #region 重新启动进程
    /// <summary>
    /// 重新启动进程
    /// </summary>
    /// <param name="pName">进程名称</param>
    /// <returns>bool</returns>
    public static bool resetProcessByPName(string pName)
    {
        bool result = false;
        if (!String.IsNullOrEmpty(pName))
        {
            try
            {
                Process[] myProcesses;
                myProcesses = Process.GetProcessesByName(pName);
                foreach (Process myProcess in myProcesses)
                {
                    myProcess.Kill();
                }
                result = true;
            }
            catch (Exception ex) { }
        }
        return result;
    }
    #endregion
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2010-12-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档