Winform怎么实现判断时间后结束指定进程除外的其他进程?急!(语言-c#)

Winform怎么实现判断时间后结束指定进程除外的其他进程

以下是已写好的代码,直接在窗体上编辑,没有其他组件。

     private void Form1_Load(object sender, EventArgs e)
        {   

            string _strWorkingDayAM = "08:00";//工作时间上午08:30

            TimeSpan dspWorkingDayAM = DateTime.Parse(_strWorkingDayAM).TimeOfDay;

            //获取当前时间
            DateTime dateTime = DateTime.Now;
            TimeSpan dspNow = dateTime.TimeOfDay;
            if (dspNow > dspWorkingDayAM)
            {
                //执行操作。。。
            }
            {

            }
            {

            }
        }

就是想一启动这个软件就检查时间,如果时间大于等于8点,就关闭除指定进程和系统进程外的所有程序,包括Windows资源管理器,22点后到7点可以照常使用。

谢谢各位了!

示例代码大概如下,自己添加,系统进程没法判断,只能自己添加的例外列表里面。要不就用黑名单制,再黑名单中的直接结束,要不杀掉系统进程有可能宕机

using System;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
namespace demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //要排除的进程名称,自己添加
            var keepsProcesses = new List<string> {
                //以下系统进程来自 https://stackoverflow.com/questions/53354644/find-out-whether-a-process-is-a-system-process
            "explorer",
            "ntoskrnl",
            "winlogon",
            "wininit",
            "csrss",
            "lsass",
            "smss",
            "services",
            "taskhost",
            "svchost",
            "sihost"
            };

            new Task(new Action(delegate {
                while (true) {
                    var hour = DateTime.Now.Hour;
                    if (hour > 7 && hour < 22)
                    {
                        var processes = Process.GetProcesses();
                        foreach (var p in processes)
                        {
                            if (!keepsProcesses.Contains(p.ProcessName))
                            {
                                try { p.Kill(); }
                                catch { }
                            }
                        }
                    }

                    Thread.CurrentThread.Join(100);//100ms检查一次
                }

            
            })).Start(); ;
        }
    }
}


img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632