C Implementation Process Management Start and Stop Instances
- 2021-07-01 08:05:09
- OfStack
This paper describes the starting and stopping methods of C # to realize process management. Share it for your reference. The specific implementation method is as follows:
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;
// Reference namespace
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();
// Display as a detailed list
listView1.View = View.Details;
// Parameter meaning: column name, width (pixels), horizontal alignment
listView1.Columns.Add(" Process ID", 70, HorizontalAlignment.Left);
listView1.Columns.Add(" Process name ", 70, HorizontalAlignment.Left);
listView1.Columns.Add(" Occupy memory ", 70, HorizontalAlignment.Left);
listView1.Columns.Add(" Startup time ", 70, HorizontalAlignment.Left);
listView1.Columns.Add(" Filename ", 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);
}
// Set the name and parameters of the application to start
ProcessStartInfo ps = new ProcessStartInfo(fileName, argument);
ps.WindowStyle = ProcessWindowStyle.Normal;
fileIndex++;
Process p = new Process();
p.StartInfo = ps;
p.Start();
// Wait for startup to complete, otherwise getting process information may fail
p.WaitForInputIdle();
RefreshListView();
}
private void buttonStop_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
// Create a new Process Array of components , And associate them with the specified process name ( Notepad ) is associated with all process resources of the .
Process[] myprocesses;
myprocesses = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
foreach (Process p in myprocesses)
{
// The process is closed by sending a closing message to the main window of the process
p.CloseMainWindow();
// Wait 1000 Milliseconds
Thread.Sleep(1000);
// Release all resources associated with this component
p.Close();
}
fileIndex = 0;
RefreshListView();
this.Cursor = Cursors.Default;
}
private void RefreshListView()
{
listView1.Items.Clear();
// Create Process Array of type , And associate them with all processes in the system
Process[] processes;
processes = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(fileName));
foreach (Process p in processes)
{
// Add the process name, physical memory occupied and process start time of each process listView Medium
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)
{
}
}
}
I hope this article is helpful to everyone's C # programming.