C# 当多个进程关闭时,如何关闭计算机?
我正在尝试制作一个自动关机应用程序,当多个进程关闭时,它将关闭计算机 示例:用户有一个复选框,其中列出了当前正在运行的所有进程。用户勾选他们希望监视的所有所需进程。一旦所有这些进程关闭,计算机就应该关机。我在执行此操作时遇到问题,我不知道如何进行程序检查,以查看已检查的流程项是否已关闭。这里是一些代码,我现在有,我将感谢所有的帮助,任何人都可以给我C# 当多个进程关闭时,如何关闭计算机?,c#,winforms,windows-7,process,shutdown,C#,Winforms,Windows 7,Process,Shutdown,我正在尝试制作一个自动关机应用程序,当多个进程关闭时,它将关闭计算机 示例:用户有一个复选框,其中列出了当前正在运行的所有进程。用户勾选他们希望监视的所有所需进程。一旦所有这些进程关闭,计算机就应该关机。我在执行此操作时遇到问题,我不知道如何进行程序检查,以查看已检查的流程项是否已关闭。这里是一些代码,我现在有,我将感谢所有的帮助,任何人都可以给我 using System; using System.Collections.Generic; using System.ComponentMode
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int counter;
Process[] p = Process.GetProcesses();
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 100;
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
counter = 0;
checkedListBox1.Items.Clear();
Process[] p = Process.GetProcesses();
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
counter = counter + 1;
}
if (counter == 0)
{
MessageBox.Show("works");
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
谢谢
-Angel Mendez假设您有一个代表复选框的
列表,请尝试:
List<string> checkProcs = new List<string>(); // All monitored process names
var allProcesses = Process.GetProcesses().Select(p => p.ProcessName);
// Now use:
allProcesses.Except(checkProcs)
List checkProcs=new List();//所有受监视的进程名称
var allprocesss=Process.getprocesss().Select(p=>p.ProcessName);
//现在使用:
所有进程。除(checkProcs)外
这应该会给出一个不再存在的受监控流程的列表
using System;
using System.Collections;
using System.Windows.Forms;
using System.Diagnostics;
namespace testprocessapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Process[] p = Process.GetProcesses();
timer1.Interval = 10000;
checkedListBox1.Items.Clear();
foreach (Process plist in p)
{
checkedListBox1.Items.Add(plist.ProcessName);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
int counter = 0;
Process[] p = Process.GetProcesses();
foreach (Process process in p)
{
foreach (var item in checkedListBox1.Items)
{
if (item.ToString() == process.ProcessName)
{
counter = counter + 1;
}
}
}
MessageBox.Show(counter == 0 ? "Your process has been terminated" : "Your process is still there");
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList arrayList = new ArrayList();
foreach (var checkedItem in checkedListBox1.CheckedItems)
{
arrayList.Add(checkedItem);
}
checkedListBox1.DataSource = arrayList;
//button1.Enabled = false;
button1.Text = "Monitoring...";
timer1.Start();
}
}
}
我现在已经创建了应用程序的副本。这段代码有效。我将使用的方法是统计计时器中所有正在运行的受监视进程。当该计数器=0时,我将启动shutdown命令。您好,感谢您的回复。我不确定我是否理解你的意思,你认为你能举个例子吗?谢谢。当您的进程完成时,只需执行一个进程。启动(“SHUTDOWN-s-t01”)。关于shutdown命令的更多信息-我喜欢这个解决方案新的C#是awesomehi,我正在使用你的方法,但是我在“foreach(checklistbox1.checkeditems中的进程pl)”中得到了一个错误,它声明“无法将'System.String'类型的对象强制转换为'System.Diagnostics.process'。“我如何解决这个问题?foreach(checkedListBox1.CheckedItems中的var pl)
我尝试了该代码,但它没有显示一个显示它工作的messagebox,不确定它可能是什么。在我的回答中,我已经取出了messagebox(抱歉)。在if(counter==0)
中添加messagebox.show(“工作”);
我知道我这样做了,但我不知道为什么它仍然不工作。