Process Implementation (using the. NET platform)

Source: Internet
Author: User

Reference: http://msdn.microsoft.com/library/chs/default.asp? Url =/library/CHS/vbcon/html/vbtskmanagingprocesses. asp

Today, we will continue to look at the process and use the. NET platform as an example of several processes.

1. Obtain the user's "My Documents" directory and the current applicationProgramDirectory (irrelevant process knowledge ):

String Mydocumentspath = Environment. getfolderpath (environment. specialfolder. Personal ); // Obtain the specific directory of the current user "My Documents"
String Currentpath = Environment. currentdirectory; // Get the current application directory

Of course, there are many attribute options under environment. specialfolder. For example:

Cookies The Directory of the public repository used as the Internet cookie.
Desktop Logical desktop, rather than physical file system location.
Desktopdirectory The directory used to physically store the file objects on the desktop.

This directory should not be confused with the Desktop Folder, which is a virtual folder.

Favorites The directory used as the public repository for user favorites items.
History The directory used as the public repository for Internet history items.
Internetcache Directory used as the public repository for Internet temporary files.
Localapplicationdata Directory, which is used as a public repository for application-specific data used by non-roaming users.
Mycomputer "My Computer" folder.
Mymusic "My music" folder.
Mypictures "My pictures" folder.

For more information, see msdn.

2. Start a process:
It is easy to start a process in. net. You only need to create a process object and set the process. startinfo option to start an external process. For example, to open a Word file in a program:

Process proc =   New Process ();
Proc. startinfo. filename =   " E: \ 1.doc " ;
Proc. startinfo. Vert =   " Open " ; // This is open. If you want to print it, you can set it to "print.
Proc. startinfo. windowstyle = Processwindowstyle. maximized; // Maximize display window

Of course, there are more simple methods, you do not need to instantiate the process object, you just need to call the static method start of process:

Processstartinfo psinfo = New Processstartinfo ();
Psinfo. filename = " E: \ 1.doc " ;
Psinfo. verb = " Print " ;
Psinfo. windowstyle = Processwindowstyle. normal;
Process. Start (psinfo );

The simplest is process. Start ("E: \ 1.doc"). An external process can be called in one sentence.

3. Close the process
There are two ways to close a process:
(1) If the process has a graphical interface window, call the closemainwindow () function.
(2) In the console window, call the kill () function.

For example, the above process can be closed as follows:
Process. windowmainwindow ();

One problem is that if you hold the process object, you can call it to close the process. But what if I do not hold the process object? Just like the second method to open a process, there is no process object at all.
Msdn solves the following problems:

Process [] processes;
Processes = process. getprocessesbyname ("winword ");
Foreach (Process in processes)
{
Process. closemainwindow ();
}

4. Determine whether the process is responding:
Call the respondingscripts of the process object. For example, if the program named notepad.exe responds, use closemainwindow to close the process. If no response is received, use kill to forcibly close the process:

Process [] notepads;
Notepads = Process. getprocessesbyname ( " Notepad.exe " );
// Test to see if the process is responding.
If (Notepads [ 0 ]. Responding)
{
Notepads [0]. Closemainwindow ();
}
Else
{
Notepads [0]. Kill ();
}

5. Check whether the process has exited:
Call the hasexit attribute of process. Then you can determine whether the process has exited.

6. instance:
An instance for enumerating all processes is implemented in. net. Everything is encapsulated.Source codeYou can list all processes in two rows.
(1) list all processes and display them in the cbprocesses object of the ComboBox type.

Cbprocesses. Items. Clear ();
Process [] processes;
Processes = Process. getprocesses ();
Foreach (Process In Processes)
{
Cbprocesses. Items. Add (process. processname );
}

(2) Many modules (DLL and exe files) are loaded in the process address space to list them. Put it in lbprocesses Of The ListBox type:

Private   Void Cbprocesses_selectedindexchanged ( Object Sender, system. eventargs E)
{
// Display process module information
Lbprocesses. Items. Clear ();
Process [] Process = Process. getprocessesbyname (cbprocesses. items [cbprocesses. selectedindex]. tostring ());
Try
{
Foreach (Processmodule amodule In Process [ 0 ]. Modules)
{
Lbprocesses. Items. Add (amodule. modulename+' '+Amodule. filename );
}
}
Catch
{
Return;
}
}

(3) display process details:
This is to call some methods in the process object to obtain its information. msdn has a detailed introduction, you can refer to msdn when necessary:

String Procpid = " PID (process identifier ): " + Process [ 0 ]. Id. tostring (); // Process identifier
String Proccpu = " Total CPU usage time: " + Process [ 0 ]. Totalprocessortime. totalminutes. tostring () + " Minutes " ; // CPU usage, expressed in minutes
String Proccputime = " Obtain the user's CPU time at this time: " + Process [ 0 ]. Userprocessortime. totalminutes. tostring () + " Minutes " ; // CPU time, expressed in minutes
String Procmemory = " Memory usage: " + (Process [ 0 ]. Workingset / 1024 ). Tostring () + " KB " ;
String Procpriority = " Priority: " + Process [ 0 ]. Basepriority. tostring ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.