【发布时间】:2012-08-15 07:39:58
【问题描述】:
我正在尝试制作一个自动关闭应用程序,该应用程序将在多个进程关闭时关闭计算机。
示例:用户有一个复选框,列出了所有当前正在运行的进程。用户选中他们希望监视的所有所需进程。一旦所有这些进程关闭,则计算机应该关闭。我在这样做时遇到了麻烦,我不知道如何让程序检查检查的进程项是否已关闭。这是我现在拥有的一些代码,我将不胜感激任何人可以给我的所有帮助。
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();
}
}
}
谢谢,
-天使门德斯
【问题讨论】:
-
我会使用的方法是在计时器中统计所有正在运行的监控进程。当该计数器 = 0 时,我将启动关机命令。
-
您好,感谢您的回复。我不确定我是否理解你的意思,你认为你可以举个例子吗?谢谢。
-
当您的流程完成后,只需执行 Process.Start("SHUTDOWN -s -t 01")。有关关闭命令的更多信息在这里 - microsoft.com/resources/documentation/windows/xp/all/proddocs/…
标签: c# winforms windows-7 process shutdown