Lab+1+进程的创建与终止

Lab+1+进程的创建与终止

ID:40559077

大小:39.50 KB

页数:4页

时间:2019-08-04

Lab+1+进程的创建与终止_第1页
Lab+1+进程的创建与终止_第2页
Lab+1+进程的创建与终止_第3页
Lab+1+进程的创建与终止_第4页
资源描述:

《Lab+1+进程的创建与终止》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、Lab1进程的创建与终止实验目的:掌握利用程序创建进程和终止进程的基本方法说明:本次实验采用.NET平台提供的Process类实现对本地进程信息的访问,以及启动和停止本地系统进程。1创建进程例1.创建一个新的进程,这个新的进程是一个记事本程序步骤:1)在代码中引入System.Diagnostics名字空间2)关键代码:varpro=newProcess();pro.StartInfo.FileName="notepad.exe";pro.Start();2.获取计算机运行中的进程信息//Getthecurrentprocess.ProcesscurrentProces

2、s=Process.GetCurrentProcess();//GetallinstancesofNotepadrunningonthelocalcomputer.Process[]localByName=Process.GetProcessesByName("notepad");//GetallinstancesofNotepadrunningonthespecifieccomputer.//1.Usingthecomputeralias(donotprecedewith"\").Process[]remoteByName=Process.GetProcessesB

3、yName("notepad","myComputer");//2.UsinganIPaddresstospecifythemachineNameparameter.Process[]ipByName=Process.GetProcessesByName("notepad","169.0.0.0");//Getallprocessesrunningonthelocalcomputer.Process[]localAll=Process.GetProcesses();//Getallprocessesrunningontheremotecomputer.Process[]

4、remoteAll=Process.GetProcesses("myComputer");//Getaprocessonthelocalcomputer,usingtheprocessid.ProcesslocalById=Process.GetProcessById(1234);//Getaprocessonaremotecomputer,usingtheprocessid.ProcessremoteById=Process.GetProcessById(2345,"myComputer");3.终止进程有2个方法:方法1)Process.CloseMainWindo

5、w方法:通过向进程的主窗口发送关闭消息来关闭拥有用户界面的进程。例子:下面的示例启动一个记事本实例。 然后它最多在10秒内,以两秒为间隔检索关联进程的物理内存使用情况。 该示例检测该进程在经过10秒后是否退出。 如果该进程在10秒后仍在运行,该示例就会将其关闭。参考代码:usingSystem;usingSystem.Diagnostics;usingSystem.Threading;namespaceProcess_Sample{classMyProcessClass{publicstaticvoidMain(){try{ProcessmyProcess;myProc

6、ess=Process.Start("Notepad.exe");//Displayphysicalmemoryusage5timesatintervalsof2seconds.for(inti=0;i<5;i++){if(!myProcess.HasExited){//Discardcachedinformationabouttheprocess.myProcess.Refresh();//Printworkingsettoconsole.Console.WriteLine("PhysicalMemoryUsage:"+myProcess.WorkingSet.ToS

7、tring());//Wait2seconds.Thread.Sleep(2000);}else{break;}}//Closeprocessbysendingaclosemessagetoitsmainwindow.myProcess.CloseMainWindow();//Freeresourcesassociatedwithprocess.myProcess.Close();}catch(Exceptione){Console.WriteLine("Thefollowingexceptionwasraised:");Console.

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。