国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#實(shí)現(xiàn)進(jìn)程管理的啟動(dòng)和停止實(shí)例

2020-01-24 01:39:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了C#實(shí)現(xiàn)進(jìn)程管理的啟動(dòng)和停止方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

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();      //以詳細(xì)列表方式顯示      listView1.View = View.Details;      //參數(shù)含義:列名稱(chēng),寬度(像素),水平對(duì)齊方式      listView1.Columns.Add("進(jìn)程ID", 70, HorizontalAlignment.Left);      listView1.Columns.Add("進(jìn)程名稱(chēng)", 70, HorizontalAlignment.Left);      listView1.Columns.Add("占用內(nèi)存", 70, HorizontalAlignment.Left);      listView1.Columns.Add("啟動(dòng)時(shí)間", 70, HorizontalAlignment.Left);      listView1.Columns.Add("文件名", 280, HorizontalAlignment.Left);    }    private void buttonStart_Click(object sender, EventArgs e)    {      string argument = Application.StartupPath + "http://myfile" + fileIndex + ".txt";      if (File.Exists(argument)==false)      {        File.CreateText(argument);      }      //設(shè)置要啟動(dòng)的應(yīng)用程序名稱(chēng)及參數(shù)      ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);      ps.WindowStyle = ProcessWindowStyle.Normal;      fileIndex++;      Process p = new Process();      p.StartInfo = ps;      p.Start();      //等待啟動(dòng)完成,否則獲取進(jìn)程信息可能會(huì)失敗      p.WaitForInputIdle();      RefreshListView();    }    private void buttonStop_Click(object sender, EventArgs e)    {      this.Cursor = Cursors.WaitCursor;      //創(chuàng)建新的Process組件的數(shù)組,并將它們與指定的進(jìn)程名稱(chēng)(Notepad)的所有進(jìn)程資源相關(guān)聯(lián).      Process[] myprocesses;      myprocesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));      foreach (Process p in myprocesses)      {        //通過(guò)向進(jìn)程主窗口發(fā)送關(guān)閉消息達(dá)到關(guān)閉進(jìn)程的目的        p.CloseMainWindow();        //等待1000毫秒        Thread.Sleep(1000);        //釋放與此組件關(guān)聯(lián)的所有資源        p.Close();      }      fileIndex = 0;      RefreshListView();      this.Cursor = Cursors.Default;    }    private void RefreshListView()    {      listView1.Items.Clear();      //創(chuàng)建Process類(lèi)型的數(shù)組,并將它們與系統(tǒng)內(nèi)所有進(jìn)程相關(guān)聯(lián)      Process[] processes;      processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));      foreach (Process p in processes)      {        //將每個(gè)進(jìn)程的進(jìn)程名稱(chēng)、占用的物理內(nèi)存以及進(jìn)程開(kāi)始時(shí)間加入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)    {    }  }}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 永春县| 子长县| 聂拉木县| 中牟县| 霞浦县| 三河市| 星子县| 南宁市| 方山县| 阿城市| 卓资县| 佛教| 宜都市| 凭祥市| 杭锦后旗| 阳泉市| 全南县| 池州市| 义马市| 孟州市| 南郑县| 九寨沟县| 正定县| 伽师县| 丹阳市| 靖宇县| 民乐县| 吉首市| 福州市| 夏津县| 手机| 台南市| 奇台县| 招远市| 杭锦后旗| 霍邱县| 尼玛县| 达孜县| 塔城市| 和林格尔县| 长治县|