实验3:进程管理练习
通过本实验,熟悉和掌握Process类的使用。
1、创建一个WPF应用程序项目
2、将App.xaml中的Application.Resources节内容改为
3、修改MainWindow.xaml及代码隐藏类
MainWindow.xaml
MainWindow.xaml.cs主要内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Button oldButton = new Button();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
btn.Foreground = Brushes.Black;
oldButton.Foreground = Brushes.Black;
oldButton = btn;
frame1.Source = new Uri(btn.Tag.ToString(), UriKind.Relative);
}
}
}
修改Page1.xaml的核心代码
Page1.xaml.cs的核心代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1.Examples
{
/// <summary>
/// Page1.xaml 的交互逻辑
/// </summary>
public partial class Page1 : Page
{
int fileIndex = 1;
string fileName = "Notepad";
List<Data> list = new List<Data>();
public Page1()
{
InitializeComponent();
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
string argument = Environment.CurrentDirectory + "\\myfile" + (fileIndex++) + ".txt";
if (File.Exists(argument)==false)
{
File.CreateText(argument);
}
Process p = new Process();
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = argument;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
p.WaitForInputIdle();
RefreshProcessInfo();
}
private void BtnStop_Click(object sender, RoutedEventArgs e)
{
this.Cursor = Cursors.Wait;
Process[] myprocesses;
myprocesses = Process.GetProcessesByName(fileName);
foreach (Process p in myprocesses)
{
using (p)
{
p.CloseMainWindow();
Thread.Sleep(1000);
p.WaitForExit();
}
}
fileIndex = 0;
RefreshProcessInfo();
this.Cursor = Cursors.Arrow;
}
private void RefreshProcessInfo()
{
dataGrid1.ItemsSource = null;
list.Clear();
Process[] processes = Process.GetProcessesByName(fileName);
foreach (Process p in processes)
{
list.Add(new Data() {
Id = p.Id,
ProcessName = p.ProcessName,
TotalMemory = string.Format("{0,10:0} KB" ,p.WorkingSet64 / 1024d),
StartTime=p.StartTime.ToString("yyyy-M-d HH:mm:ss"),
FileName=p.MainModule.FileName
});
}
dataGrid1.ItemsSource = list;
}
}
public class Data
{
public int Id { get; set; }
public string ProcessName { get; set; }
public string TotalMemory { get; set; }
public string StartTime { get; set; }
public string FileName { get; set; }
}
}
实验结果
实验结果能启动多个进程,并停止所有进程
通过本实验,对Process类的使用,启动进程,停止进程,获取所有进程信息,获取指定进程信息,更加熟练和掌握了进程管理知识。