全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Lab 1 进程的创建与终止实验目的:掌握利用程序创建进程和终止进程的基本方法说明:本次实验采用.NET平台提供的Process类实现对本地进程信息的访问,以及启动和停止本地系统进程。1创建进程例1. 创建一个新的进程,这个新的进程是一个记事本程序步骤:1)在代码中引入System.Diagnostics名字空间2)关键代码:var pro = new Process(); pro.StartInfo.FileName = notepad.exe;pro.Start();2.获取计算机运行中的进程信息 / Get the current process.Process currentProcess = Process.GetCurrentProcess();/ Get all instances of Notepad running on the local computer.Process localByName = Process.GetProcessesByName(notepad);/ Get all instances of Notepad running on the specifiec computer./ 1. Using the computer alias (do not precede with ).Process remoteByName = Process.GetProcessesByName(notepad, myComputer);/ 2. Using an IP address to specify the machineName parameter. Process ipByName = Process.GetProcessesByName(notepad, );/ Get all processes running on the local computer.Process localAll = Process.GetProcesses();/ Get all processes running on the remote computer.Process remoteAll = Process.GetProcesses(myComputer);/ Get a process on the local computer, using the process id.Process localById = Process.GetProcessById(1234);/ Get a process on a remote computer, using the process id.Process remoteById = Process.GetProcessById(2345, myComputer);3.终止进程有2个方法:方法1)Process.CloseMainWindow 方法:通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。例子:下面的示例启动一个记事本实例。然后它最多在 10 秒内,以两秒为间隔检索关联进程的物理内存使用情况。该示例检测该进程在经过 10 秒后是否退出。如果该进程在 10 秒后仍在运行,该示例就会将其关闭。参考代码:using System;using System.Diagnostics;using System.Threading;namespace Process_Sample class MyProcessClass public static void Main() try Process myProcess; myProcess = Process.Start(Notepad.exe); / Display physical memory usage 5 times at intervals of 2 seconds. for (int i = 0;i 5; i+) if (!myProcess.HasExited) / Discard cached information about the process. myProcess.Refresh(); / Print working set to console. Console.WriteLine(Physical Memory Usage: + myProcess.WorkingSet.ToString(); / Wait 2 seconds. Thread.Sleep(2000); else break; / Close process by sending a close message to its main window. myProcess.CloseMainWindow(); / Free resources associated with process. myProcess.Close(); catch(Exception e) Console.WriteLine(The following exception was raised: ); Console.WriteLine(e.Message); 方法2:Process.Kill:Kill强制终止进程,而CloseMainWindow只是请求终止。参考代码:下面代码是把所有的记事本进程统统枪毙Process pro2Kill = Process.GetProcessesByName(notepad);foreach (Process p in pro2Kill) p.Kill(); foreach (var p in remoteAll) Console.WriteLine(0,1, p.ProcessName, p.Id); int k; Console.WriteLine(请?输?入?结束?进?程id); k=int.Parse(Console.ReadLine(); while (k != 0) var pk = Process.GetProcessById(k); pk.Kill(); remoteAll = Process.GetProcesses(); foreach (var p in remot
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 园林工程栽植承包合同(3篇)
- 2025年民宿税务筹划服务协议
- 2025年民宿沉浸式推广协议
- 2025年环保型塑料替代品研发项目可行性研究报告及总结分析
- 电焊工证-上岗证考试试题题库含答案参考50
- 2025年儿童智能玩具市场发展可行性研究报告及总结分析
- 2025年智能消费电子产品市场调研可行性研究报告及总结分析
- 2025年医疗保健行业医疗健康品牌形象塑造案例分析报告
- 2025年江苏省市政质量员技能认定理论考试题库 含答案
- 2025年老人陪护合同协议
- 单管塔刚性短柱基础
- 大易通用能力测评题库
- 中医诊断四诊合参
- 武汉万科商品房交付标准化工作手册2.0版
- 食品安全考试试题及答案2021
- 郦道元《水经注·序》原文翻译注释与鉴赏
- 数独题目中级90题(后附答案)
- 西门子s71500系列系统手册
- 腹直肌分离康复(产后康复课件PPT)
- 携手共育 静待花开 家长会课件
- 酒驾处罚书格式(标准版)
评论
0/150
提交评论