C#進程管理啟動和停止


 

C#進程的開始和結束,源碼如下,謝謝

轉載請注明出處http://www.cnblogs.com/minotmin/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
//引用命名空間
using System.Diagnostics;
using System.Threading;


namespace StartStopProcess
{
    public partial class Form1 : Form
    {
        int fileIndex;
        string fileName = "Notepad.exe";
        Process process1 = new Process();
        public Form1()
        {
            InitializeComponent();
            //以詳細列表方式顯示
            listView1.View = View.Details;
            //參數含義:列名稱,寬度(像素),水平對齊方式
            listView1.Columns.Add("進程ID", 70, HorizontalAlignment.Left);
            listView1.Columns.Add("進程名稱", 70, HorizontalAlignment.Left);
            listView1.Columns.Add("占用內存", 70, HorizontalAlignment.Left);
            listView1.Columns.Add("啟動時間", 70, HorizontalAlignment.Left);
            listView1.Columns.Add("文件名", 280, HorizontalAlignment.Left);
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            string argument = Application.StartupPath + "\\myfile" + fileIndex + ".txt";
            if (File.Exists(argument)==false)
            {
                File.CreateText(argument);
            }
            //設置要啟動的應用程序名稱及參數
            ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);
            ps.WindowStyle = ProcessWindowStyle.Normal;
            fileIndex++;
            Process p = new Process();
            p.StartInfo = ps;
            p.Start();
            //等待啟動完成,否則獲取進程信息可能會失敗
            p.WaitForInputIdle();
            RefreshListView();
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            //創建新的Process組件的數組,並將它們與指定的進程名稱(Notepad)的所有進程資源相關聯.
            Process[] myprocesses;
            myprocesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
            foreach (Process p in myprocesses)
            {
                //通過向進程主窗口發送關閉消息達到關閉進程的目的
                p.CloseMainWindow();
                //等待1000毫秒
                Thread.Sleep(1000);
                //釋放與此組件關聯的所有資源
                p.Close();
            }
            fileIndex = 0;
            RefreshListView();
            this.Cursor = Cursors.Default;
        }

        private void RefreshListView()
        {
            listView1.Items.Clear();
            //創建Process類型的數組,並將它們與系統內所有進程相關聯
            Process[] processes;
            processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
            foreach (Process p in processes)
            {
                //將每個進程的進程名稱、占用的物理內存以及進程開始時間加入listView中
                ListViewItem item = new ListViewItem(
                    new string[]{
                        p.Id.ToString(),
                        p.ProcessName,
                        string.Format("{0} KB", p.PrivateMemorySize64/1024.0f),
                        string.Format("{0}",p.StartTime),
                        p.MainModule.FileName
                    });
                listView1.Items.Add(item);
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            RefreshListView();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM