C#實(shí)現(xiàn)進(jìn)程管理的啟動和停止實(shí)例
更新時間:2015年07月02日 17:48:24 作者:程序猴
這篇文章主要介紹了C#實(shí)現(xiàn)進(jìn)程管理的啟動和停止方法,以操作記事本程序?yàn)槔?實(shí)例分析了C#針對進(jìn)程操作的基本技巧,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)進(jìn)程管理的啟動和停止方法。分享給大家供大家參考。具體實(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ù)含義:列名稱,寬度(像素),水平對齊方式
listView1.Columns.Add("進(jìn)程ID", 70, HorizontalAlignment.Left);
listView1.Columns.Add("進(jìn)程名稱", 70, HorizontalAlignment.Left);
listView1.Columns.Add("占用內(nèi)存", 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);
}
//設(shè)置要啟動的應(yīng)用程序名稱及參數(shù)
ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);
ps.WindowStyle = ProcessWindowStyle.Normal;
fileIndex++;
Process p = new Process();
p.StartInfo = ps;
p.Start();
//等待啟動完成,否則獲取進(jìn)程信息可能會失敗
p.WaitForInputIdle();
RefreshListView();
}
private void buttonStop_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
//創(chuàng)建新的Process組件的數(shù)組,并將它們與指定的進(jìn)程名稱(Notepad)的所有進(jìn)程資源相關(guān)聯(lián).
Process[] myprocesses;
myprocesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
foreach (Process p in myprocesses)
{
//通過向進(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類型的數(shù)組,并將它們與系統(tǒng)內(nèi)所有進(jìn)程相關(guān)聯(lián)
Process[] processes;
processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
foreach (Process p in processes)
{
//將每個進(jìn)程的進(jìn)程名稱、占用的物理內(nèi)存以及進(jìn)程開始時間加入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#程序設(shè)計有所幫助。
您可能感興趣的文章:
- C#實(shí)現(xiàn)將應(yīng)用程序設(shè)置為開機(jī)啟動的方法
- C#設(shè)置開機(jī)啟動項(xiàng)、取消開機(jī)啟動項(xiàng)
- C#代碼設(shè)置開機(jī)啟動示例
- c# 開機(jī)啟動項(xiàng)的小例子
- C#實(shí)現(xiàn)開機(jī)自動啟動設(shè)置代碼分享
- C#實(shí)現(xiàn)啟動,關(guān)閉與查找進(jìn)程的方法
- C#實(shí)現(xiàn)在啟動目錄創(chuàng)建快捷方式的方法
- C#啟動進(jìn)程的幾種常用方法
- c#制作簡單啟動畫面的方法
- C#實(shí)現(xiàn)程序開機(jī)啟動的方法
相關(guān)文章
深入淺析C#?11?對?ref?和?struct?的改進(jìn)
這篇文章主要介紹了C#?11?對?ref?和?struct?的改進(jìn),有了這些基礎(chǔ)設(shè)施,開發(fā)者們將能輕松使用安全的方式來編寫沒有任何堆內(nèi)存開銷的高性能代碼,需要的朋友可以參考下2022-04-04
解析OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線問題
這篇文章主要介紹了OpenXml?Pptx的邊框虛線轉(zhuǎn)為WPF的邊框虛線,在文中用PPTX的7種直線,分別設(shè)置7種能夠設(shè)置的虛線類型,具體實(shí)例代碼跟隨小編一起看看吧2021-12-12
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04
C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯密碼賬戶鎖定
這篇文章介紹了C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯密碼賬戶鎖定的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04
C#迭代器模式(Iterator Pattern)實(shí)例教程
這篇文章主要介紹了C#迭代器模式(Iterator Pattern),包括了迭代器的適用范圍及用法實(shí)例,需要的朋友可以參考下2014-09-09
C# 實(shí)現(xiàn)特殊字符快速轉(zhuǎn)碼
這篇文章主要介紹了C# 實(shí)現(xiàn)特殊字符快速轉(zhuǎn)碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

