• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#通过“委托和事件”的方式实现进程监控并与“普通方式”对比 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

今天重新学习了一下观察者模式,对我的思路产生了启发。进程监控程序之前写过几个,这回换一种思路,改用委托和事件来实现。我已经用序号将关键的几步标注,方便大家理顺思路。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessMonitor
{
    public partial class ProcessMonitorForm : Form
    {
        public ProcessMonitorForm()
        {
            InitializeComponent();
            //Add the processes into the combox.
            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                processComboBox.Items.Add(process.ProcessName.ToString());
            }
        }
        //The method that starts the monitor.
        private void startButton_Click(object sender, EventArgs e)
        {
            //4.Register the monitor.
            ProcessExit += new ProcessMonitor(ProExit);
            //Start the check.
            CheckProcess();
        }
        //The mothod that checks the process.
        private void CheckProcess()
        {
            bool flag = true;
            do
            {
                var processes = Process.GetProcesses();
                int count = 0;
                foreach (var process in processes)
                {
                    if (string.Compare(process.ProcessName, processComboBox.Text, true) == 0)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    //5.The event appears.
                    ProcessExit(this, new EventArgs());
                    flag = false;
                }
            } while (flag);
        }
        //1.The delegate that monitor the process.
        public delegate void ProcessMonitor(object sender, EventArgs strEventArg);
        //2.The event that encapsulates the delegate.
        public event ProcessMonitor ProcessExit;
        //3.The method that the delegate calls.
        private void ProExit(object sender, EventArgs strEventArg)
        {
            MessageBox.Show("The target process has been dispeared.");
        }
    }
}

为了不长篇累牍,效果只是简单实现,实际工作中可以随便扩展(选择进程,点击Start按钮进行监控。):

目标程序消失后弹出提示:

再附上一个脱去委托和事件的版本,代码如下(实现效果相同):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessMonitor
{
    public partial class ProcessMonitorForm : Form
    {
        public ProcessMonitorForm()
        {
            InitializeComponent();
            //Add the processes into the combox.
            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                processComboBox.Items.Add(process.ProcessName.ToString());
            }
        }
        //The method that starts the monitor.
        private void startButton_Click(object sender, EventArgs e)
        {
            //Start the check.
            CheckProcess();
        }
        //The mothod that checks the process.
        private void CheckProcess()
        {
            bool flag = true;
            do
            {
                var processes = Process.GetProcesses();
                int count = 0;
                foreach (var process in processes)
                {
                    if (string.Compare(process.ProcessName, processComboBox.Text, true) == 0)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    ProExit();
                    flag = false;
                }
            } while (flag);
        }
        private void ProExit()
        {
            MessageBox.Show("The target process has been dispeared.");
        }
    }
}

如果用Action内置委托类型来完成的话就更方便了,代码如下(已经用序号标注关键步骤):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessMonitor
{
    public partial class ProcessMonitorForm : Form
    {
        public ProcessMonitorForm()
        {
            InitializeComponent();
            //Add the processes into the combox.
            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                processComboBox.Items.Add(process.ProcessName.ToString());
            }
        }
        //The method that starts the monitor.
        private void startButton_Click(object sender, EventArgs e)
        {
            //Start the check.
            CheckProcess();
        }
        //The mothod that checks the process.
        private void CheckProcess()
        {
            //1.Define the action.
            Action<string> processExit = s => MessageBox.Show(s);
            bool flag = true;
            do
            {
                var processes = Process.GetProcesses();
                int count = 0;
                foreach (var process in processes)
                {
                    if (string.Compare(process.ProcessName, processComboBox.Text, true) == 0)
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
                    //2.Active the action.
                    processExit("The target process has been dispeared.");
                    flag = false;
                }
            } while (flag);
        }
    }
}

脱去委托和事件的版本代码量明显比用委托和事件的代码量少了,为什么我们还要选择用委托和事件来做这件事呢?到底什么情况下,更适合用委托和事件的方式来完成?书中说,委托可以提高方法扩展性,没错,是这样的,说白了就是因为更高级!个人意见哈,在代码量少,以后不需要扩展方法的情况下,用不着用委托和事件的方式去完成,直接调用方法就好了。如果我说错了,欢迎指正我。

利用C#6.0中的语法糖扩展方法来替代foreach循环,代码量将更少。代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProcessMonitor
{
    public partial class ProcessMonitorForm : Form
    {
        public ProcessMonitorForm()
        {
            InitializeComponent();
            //Add the processes into the combox.
            var processes = Process.GetProcesses();
            foreach (var process in processes)
            {
                processComboBox.Items.Add(process.ProcessName.ToString());
            }
        }
        //The method that starts the monitor.
        private void startButton_Click(object sender, EventArgs e)
        {
            //Start the check.
            CheckProcess();
        }
        //The mothod that checks the process.
        private void CheckProcess()
        {
            //1.Define the action.
            Action<string> processExit = s => MessageBox.Show(s);
            bool flag = true;
            do
            {
                var processes = Process.GetProcesses();
                var countVar = processes.Where(i => i.ProcessName == processComboBox.Text);
                if (countVar.Count() == 0)
                {
                    //2.Active the action.
                    processExit("The target process has been dispeared.");
                    flag = false;
                }
            } while (flag);
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#编码规范(2):详细命名规则及编码习惯发布时间:2022-07-13
下一篇:
C#Dictionary判断Key是否存在发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap