0

複数のプロセスが閉じたときにコンピューターをシャットダウンする自動シャットダウン アプリケーションを作成しようとしています。

例: ユーザーは、現在実行中のすべてのプロセスを一覧表示するチェックリスト ボックスを持っています。ユーザーのチェックは、監視したいすべてのプロセスにマークを付けます。これらのプロセスがすべて閉じられると、コンピューターはシャットダウンすることになっています。これを行うのに問題があります。チェックしたプロセス項目が閉じているかどうかをプログラムにチェックさせる方法がわかりません。これが私が今持っているコードです。誰かが私に与えることができるすべての助けに感謝します。

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();
        }



    }
}

ありがとう、

-エンジェル・メンデス

4

2 に答える 2

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)

これにより、もはや存在しない監視対象プロセスのリストが表示されます。

于 2012-08-15T08:14:49.313 に答える
0
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();


        }
    }
}

これで、アプリケーションのレプリカが作成されました。このコードは機能します。

于 2012-08-15T08:16:10.030 に答える