C#編程應用–進程管理

來源:互聯網
上載者:User

一年多沒接觸C#開發了,複習一下Winform方面的知識:

 

 C#開啟、關閉進程,擷取進程列表;

 

有圖有真相:

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.Diagnostics;
 9 using System.Threading;
10 
11 namespace ProcessExample
12 {
13 
14     public partial class Form1 : Form
15     {
16 
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void buttonStart_Click(object sender, EventArgs e)
23         {
24             process1.StartInfo.FileName = "notepad.exe";
25             //啟動Notepad.exe進程.
26             process1.Start();
27         }
28         private void buttonStop_Click(object sender, EventArgs e)
29         {
30             //建立新的Process組件的數組,並將它們與指定的進程名稱(Notepad)的所有進程資源相關聯.
31             Process[] myprocesses;
32             myprocesses = Process.GetProcessesByName("Notepad");
33             foreach (Process instance in myprocesses)
34             {
35                 //設定終止當前線程前等待1000毫秒
36                 instance.WaitForExit(1000);
37                 instance.CloseMainWindow();
38             }
39         }
40         private void buttonView_Click(object sender, EventArgs e)
41         {
42             listBox1.Items.Clear();
43             //建立Process類型的數組,並將它們與系統內所有進程相關聯
44             Process[] processes;
45             processes = Process.GetProcesses();
46             foreach (Process p in processes)
47             {
48                 //Idle指顯示CPU空閑率的進程名稱
49                 //由於訪問Idle的StartTime會出現異常,所以將其排除在外
50                 if (p.ProcessName != "Idle")
51                 {
52                     //將每個進程名和進程開始時間加入listBox1中
53                     this.listBox1.Items.Add(
54                     string.Format("{0,-30}{1:h:m:s}", p.ProcessName, p.StartTime));
55                 }
56             }
57         }
58     }
59 }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.