Introduction to the usage of the process class in C #

Source: Internet
Author: User

Inheritance hierarchy

--------------------------------------------------------------------------------

System. Object
System. externalbyrefobject
System. ComponentModel. Component
System. Diagnostics. Process


Namespace: System. Diagnostics
Assembly: System (in System. dll)
Syntax

--------------------------------------------------------------------------------

 
[PermissionSetAttribute (SecurityAction. InheritanceDemand, Name = "FullTrust")]
[HostProtectionAttribute (SecurityAction. LinkDemand, SharedState = true, Synchronization = true,
ExternalProcessMgmt = true, SelfAffectingProcessMgmt = true)]
[PermissionSetAttribute (SecurityAction. LinkDemand, Name = "FullTrust")]
Public class Process: Component
The Process type exposes the following members.

Constructor

--------------------------------------------------------------------------------

Description
Process initializes a new instance of the Process class.
Attribute

--------------------------------------------------------------------------------

Description
BasePriority gets the basic priority of the associated process.
CanRaiseEvents gets a value indicating whether a component can trigger an event. (Inherited from Component .)
The Container obtains the IContainer, which contains the Component. (Inherited from Component .)
DesignMode gets a value to indicate whether the Component is in the design mode. (Inherited from Component .)
EnableRaisingEvents gets or sets whether the Exited event should be triggered when the process is terminated.
Events gets the list of Event Handlers appended to this Component. (Inherited from Component .)
ExitCode gets the value specified when the associated process is terminated.
ExitTime gets the time when the associated process exits.
Handle gets the local Handle of the associated process.
HandleCount gets the number of handles opened by the process.
HasExited gets the value indicating whether the associated process has been terminated.
Id to obtain the unique identifier of the associated process.
MachineName gets the name of the computer on which the associated process is running.
MainModule obtains the main module of the associated process.
Main1_whandle gets the window handle of the Main Window of the associated process.
MainWindowTitle: Obtain the title of the Main Window of the process.
MaxWorkingSet obtains or sets the maximum allowable working set size of associated processes.
MinWorkingSet obtains or sets the minimum allowable working set size of the associated process.
Modules gets the Modules loaded by the associated process.
NonpagedSystemMemorySize is out of date. Obtain the size of the system memory allocated to this process.
NonpagedSystemMemorySize64 gets the amount of non-paging system memory allocated for the associated process.
PagedMemorySize is out of date. Obtains the size of the paging memory.
PagedMemorySize64 obtains the amount of paging memory allocated for the associated process.
PagedSystemMemorySize is out of date. Obtains the system memory size of a page.
PagedSystemMemorySize64 obtains the amount of paging system memory allocated for the associated process.
PeakPagedMemorySize is out of date. Obtains the peak paging memory size.
PeakPagedMemorySize64 obtains the maximum amount of memory in the virtual memory paging file used by the associated process.
PeakVirtualMemorySize is out of date. Obtains the peak virtual memory size.
PeakVirtualMemorySize64 obtains the maximum virtual memory used by the associated process.
PeakWorkingSet is out of date. Obtains the peak working set size of the associated process.
PeakWorkingSet64 obtains the maximum amount of physical memory used by the associated process.
PriorityBoostEnabled gets or sets a value that indicates whether the operating system temporarily improves the priority of associated processes when the main window has focus.
PriorityClass gets or sets the overall priority category of the associated process.
PrivateMemorySize is out of date. Obtain the size of the dedicated memory.
PrivateMemorySize64 gets the amount of dedicated memory allocated for the associated process.
PrivilegedProcessorTime gets the privileged processor time of this process.
ProcessName: get the name of the process.
ProcessorAffinity gets or sets some processors. threads in this process can run on these processors as planned.
Responding gets the value indicating whether the user interface of the process is Responding.
SessionId: obtains the Terminal Service session identifier of the associated process.
Site to obtain or set the ISite of Component. (Inherited from Component .)
StandardError gets the stream used to read the error output of the application.
StandardInput gets the stream used to write the input to the application.
StandardOutput gets the stream used to read application output.
StartInfo gets or sets the attribute of the Start method to be passed to Process.
StartTime gets the start time of the associated process.
SynchronizingObject gets or sets the object used to block the event processing program called by the process exit event.
Threads gets a group of Threads running in the associated process.
TotalProcessorTime gets the total processor time of the process.
UserProcessorTime gets the user processor time of this process.
VirtualMemorySize is out of date. Obtains the virtual memory size of a process.
VirtualMemorySize64 gets the virtual memory allocated for the associated process.
WorkingSet is out of date. Obtain the physical memory usage of the associated process.
WorkingSet64 gets the amount of physical memory allocated for the associated process.
Method

--------------------------------------------------------------------------------

Description
BeginErrorReadLine starts asynchronous read operations on the application's redirected StandardError stream.
BeginOutputReadLine starts asynchronous read operations on the application's redirected StandardOutput stream.
CancelErrorRead cancels the asynchronous read operation performed on the application's redirected StandardError stream.
CancelOutputRead cancels asynchronous read operations on the application's redirected StandardOutput stream.
Close to release all resources associated with this component.
CloseMainWindow closes a process with a user interface by sending a closing message to the main window of the process.
CreateObjRef creates an object that contains all the information required to generate a proxy for communication with a remote object. (Inherited from MarshalByRefObject .)
Dispose () releases all resources used by Component. (Inherited from Component .)
The basic structure of Dispose (Boolean. Release all resources used by this process. (Override Component. Dispose (Boolean ).)
EnterDebugMode enables SeDebugPrivilege, the local attribute of the current thread, and places the Process component in the state of interacting with the operating system Process running in special mode.
Equals (Object) determines whether the specified Object is equal to the current Object. (Inherited from Object .)
Finalize releases unmanaged resources and performs other cleanup operations before it recycles Component. (Inherited from Component .)
GetCurrentProcess gets the new Process component and associates it with the active Process.
GetHashCode is used as a hash function of a specific type. (Inherited from Object .)
GetLifetimeService searches the service objects that control the current lifetime of the Instance's lifetime policy. (Inherited from MarshalByRefObject .)
Getprocpolicyid (Int32) returns the new Process component (given the identifier of a Process on the local computer ).
Getprocpolicyid (Int32, String) returns the new Process component (given the Process identifier and the name of the computer on the network ).
GetProcesses () creates a new Process component for each Process resource on the local computer.
GetProcesses (String) creates a new Process component for each Process resource on the specified computer.
GetProcessesByName (String) creates an array of new Process components and associates them with all Process resources that share the specified Process name on the local computer.
GetProcessesByName (String, String) creates an array of new Process components and associates them with all Process resources that share the specified Process name on the remote computer.
GetService returns an object that represents the service provided by Component or its Container. (Inherited from Component .)
GetType: Get the Type of the current instance. (Inherited from Object .)
InitializeLifetimeService gets the lifetime service object that controls the lifetime policy of this instance. (Inherited from MarshalByRefObject .)
Kill immediately stops the associated process.
LeaveDebugMode enables the Process component to exit the state that allows it to interact with the operating system Process running in special mode.
MemberwiseClone () creates a superficial copy of the current Object. (Inherited from Object .)
MemberwiseClone (Boolean) creates a superficial copy of the currently stored albyrefobject object. (Inherited from MarshalByRefObject .)
OnExited triggers the Exited event.
Refresh discards any information about the associated process that has been cached in the process component.
Start () Start (or reuse) The Process resource specified by the StartInfo attribute of this Process component and associate it with this component.
Start (ProcessStartInfo) starts the Process resource specified by the parameter that contains the Process startup information (for example, the name of the Process to be started) and associates the resource with the new Process component.
Start (String) starts a Process resource by specifying the name of the document or application file and associates the resource with the new Process component.
Start (String, String) starts a Process resource by specifying the application name and a set of command line parameters, and associates the resource with the new Process component.
Start (String, String, SecureString, String) starts a Process resource by specifying the application name, user name, password, and domain, and associates the resource with the new Process component.
Start (String, SecureString, String) starts a process resource by specifying the application name, a group of command line parameters, user name, password, and domain, and associate the resource with the new Process component.
If ToString applies, the process name is formatted as a string and combined with the type of the parent component. (Override Component. ToString ().)
WaitForExit () indicates that the Process component waits for the associated Process to exit indefinitely.
WaitForExit (Int32) indicates that the Process component waits for the associated Process to exit within the specified milliseconds.
WaitForInputIdle () enables the Process component to wait for the associated Process to enter Idle State indefinitely. This overload is only applicable to processes with user interfaces and message loops.
WaitForInputIdle (Int32) enables the Process component to wait for the associated Process to enter idle state within the specified milliseconds. This overload is only applicable to processes with user interfaces and message loops.
Event

--------------------------------------------------------------------------------

Description
Disposed occurs when a component is released by calling the Dispose method. (Inherited from Component .)
ErrorDataReceived occurs when an application writes to its redirected StandardError stream.
Exited occurs when the process exits.
OutputDataReceived occurs when an application writes to its redirected StandardOutput stream.
Remarks

--------------------------------------------------------------------------------

Note:
Since. NET Framework 2.0, many. NET Framework methods and attributes have been used to reference performance counter data on other computers. This change improves performance and allows non-administrators to use the Process class. Therefore, some applications that cannot obtain exceptions in earlier versions of. NET Framework can now obtain NotSupportedException. The number of affected methods and attributes is too large to be listed here, but the exception information has been added to the subject of the affected member.
 

The Process component provides access to processes running on the computer. In the simplest words, a process is the currently running application. A thread is the basic unit for the operating system to allocate processor time to it. Code of any part of a thread executable process, including the part currently executed by another thread.

The Process component is a useful tool for starting, stopping, controlling, and monitoring applications. You can use the Process component to obtain a list of running processes or start a new Process. The Process component is used to access system processes. After the Process component is initialized, you can use this component to obtain information about the currently running Process. This information includes the thread set, loaded modules (. dll and. exe files), and performance information (such as the amount of memory currently used by the process ).

If a PATH variable is declared with quotation marks in the system, the path must be fully qualified when any process in this position is started. Otherwise, the path cannot be found. For example, if c: \ mypath is not in your path and you add it with quotation marks (path = % path %; "c: \ mypath"), start c: you must limit any processes in \ mypath.

The process component also obtains information about a group of attributes. After the Process component obtains information about one member in any group, it caches the values of other attributes in the group and before you call the Refresh method, do not obtain new information about other members in the group. Therefore, it is not guaranteed that the attribute value is compared with the last call update of the Refresh method. Group segmentation is related to the operating system.

A system process is uniquely identified by its process identifier. Like many Windows resources, a process is identified by its handle, which may not be unique on a computer. A handle is a general term used to indicate a resource identifier. Even if the Process has exited, the operating system still keeps the Process Handle, which is accessed through the Handle attribute of the Process component. Therefore, you can obtain the management information of processes, such as ExitCode (usually, or zero indicates success, or non-zero error code) and ExitTime. Handle is a very valuable resource, So handle leakage is more harmful than memory leakage.

Note:
This class includes a link requirement and an inheritance requirement at the class level applied to all members. If the caller or derived class does not have full trust permission, SecurityException is thrown. For more information about security requirements, see link requirements and inheritance requirements.
 

Note:
The HostProtectionAttribute attribute applied to this type or member has the following Resources attribute values: SharedState | Synchronization | ExternalProcessMgmt | SelfAffectingProcessMgmt. HostProtectionAttribute does not affect desktop applications (desktop applications are generally started by double-clicking the icon, typing a command, or entering a URL in a browser ). For more information, see HostProtectionAttribute or SQL Server programming and host protection features.
 

Example

--------------------------------------------------------------------------------

The following example uses an instance of the Process class to start the Process.

Using System;
Using System. Diagnostics;
Using System. ComponentModel;

Namespace MyProcessSample
{
Class MyProcess
{
Public static void Main ()
{
Process myProcess = new Process ();

Try
{
MyProcess. StartInfo. UseShellExecute = false;
// You can start any process, HelloWorld is a do-nothing example.
MyProcess. StartInfo. FileName = "C: \ HelloWorld.exe ";
MyProcess. StartInfo. CreateNoWindow = true;
MyProcess. Start ();
// This code assumes the process you are starting will terminate itself.
// Given that is started without a window so you cannot terminate it
// On the desktop, it must terminate itself or you can do it programmatically
// From this application using the Kill method.
}
Catch (Exception e)
{
Console. WriteLine (e. Message );
}
}
}
}


The following example uses the Process class and static Start method to Start a Process.

Using System;
Using System. Diagnostics;
Using System. ComponentModel;

Namespace MyProcessSample
{
Class MyProcess
{
// Opens the Internet Explorer application.
Void OpenApplication (string myFavoritesPath)
{
// Start Internet Explorer. Defaults to the home page.
Process. Start ("IExplore.exe ");

// Display the contents of the favorites folder in the browser.
Process. Start (myFavoritesPath );
}

// Opens urls and. html urls using Internet Explorer.
Void OpenWithArguments ()
{
// Url's are not considered documents. They can only be opened
// By passing them as arguments.
Process. Start ("IExplore.exe", "www.northwindtraders.com ");

// Start a Web page using a browser associated with. html and. asp files.
Process. Start ("IExplore.exe", "C: \ myPath \ myFile.htm ");
Process. Start ("IExplore.exe", "C: \ myPath \ myFile. asp ");
}

// Uses the ProcessStartInfo class to start new processes,
// Both in a minimized mode.
Void OpenWithStartInfo ()
{
ProcessStartInfo startInfo = new ProcessStartInfo ("ipolice.exe ");
StartInfo. WindowStyle = ProcessWindowStyle. Minimized;

Process. Start (startInfo );

StartInfo. Arguments = "www.northwindtraders.com ";

Process. Start (startInfo );
}

Static void Main ()
{
// Get the path that stores favorite links.
String myFavoritesPath =
Environment. GetFolderPath (Environment. SpecialFolder. Favorites );

MyProcess myProcess = new MyProcess ();

MyProcess. OpenApplication (myFavoritesPath );
MyProcess. OpenWithArguments ();
MyProcess. OpenWithStartInfo ();
}
}
}


Version Information

--------------------------------------------------------------------------------

. NET Framework
Supported by the following versions: 4, 3.5, 3.0, 2.0, 1.1, 1.0

. NET Framework Client Profile
Supported by the following versions: 4 and 3.5 SP1

. NET Framework Security

--------------------------------------------------------------------------------

LinkDemand
Used for direct callers with full trust. This class cannot be used by some trusted code.

InheritanceDemand
Used to fully trust the successor. This class cannot be inherited by some trusted code.

Platform www.2cto.com --------------------------------------------------------------------------------

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (does not support Server cores ), windows Server 2008 R2 (supports SP1 or higher Server cores), Windows Server 2003 SP2

 

. NET Framework does not support all versions of each platform. For a list of supported versions, see. NET Framework system requirements.
Thread Security

--------------------------------------------------------------------------------

All public static members of this type (Shared in Visual Basic) are thread-safe. However, it is not guaranteed that all instance members are thread-safe.

Author: a125138

Related Article

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.