【问题标题】:how do I shutdown my computer when multiple processes close?多个进程关闭时如何关闭计算机?
【发布时间】: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


【解决方案1】:

假设您有一个 List<string> 代表您的复选框,请尝试:

List<string> checkProcs = new List<string>(); // All monitored process names
var allProcesses = Process.GetProcesses().Select(p => p.ProcessName);

// Now use:
allProcesses.Except(checkProcs)

这应该给出一个不再存在的受监控进程的列表。

【讨论】:

  • 我喜欢这个解决方案,新的 C# 很棒
【解决方案2】:
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();


        }
    }
}

我现在已经创建了应用程序的副本。此代码有效。

【讨论】:

  • 嗨,我正在使用您的方法,但我收到“foreach(checklistbox1.checkeditems 中的进程 pl)”错误,它指出“无法将类型为 'System.String' 的对象强制转换为类型'System.Diagnostics.Process'。”我该如何解决这个问题?
  • foreach (var pl in checkedListBox1.CheckedItems)
  • 我尝试了代码,但它没有显示一个消息框显示它有效,不确定它可能是什么。
  • 在我的回复中,我已将 MessageBox 取出(抱歉)。在if(counter ==0)中添加MessageBox.Show("works");
  • 我知道我这样做了,但我不知道为什么它仍然不起作用。
猜你喜欢
  • 2015-09-09
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 2010-09-11
相关资源
最近更新 更多