C#完成過程治理的啟動和停滯實例。本站提示廣大學習愛好者:(C#完成過程治理的啟動和停滯實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成過程治理的啟動和停滯實例正文
本文實例講述了C#完成過程治理的啟動和停滯辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
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) { } } }
願望本文所述對年夜家的C#法式設計有所贊助。