C # Start other processes

Source: Internet
Author: User

1. Start an independent process and use the following namespace: using system. diagnostics; process class: process; process-related parameter Information Class: processstartinfo

2. Console app code waiting for startup:

Using system;
Using system. Threading;
Namespace showconsoleapp
{
Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("app start! ");
Foreach (string item in ARGs)
{
Console. writeline ("accept a Arg that is {0}", item );

Thread. Sleep (3000 );
}

Console. writeline ("app stop! ");
}
}
}

3. Startup Mode: parallel and serial modes. Compare the differences between codes.

Using system;
Using system. Threading;
Using system. diagnostics;

Namespace hdtest
{
Class Program
{
Static void main (string [] ARGs)
{

For (INT I = 0; I <2; I ++)
{
// Parallel execution: Multiple same-life instance processes are executed together.
Runmutilinstanceprocess (I );

// Serial. After a process is started, run the next
// Waitsonprocess (I );

Thread. Sleep (2000 );

}

Console. Readline ();
}

Static void runmutilinstanceprocess (int I)
{
String apppath = @ "E: \ vs2010code \ apptest \ showconsoleapp \ bin \ debug \ showconsoleapp.exe ";
Processstartinfo process = new processstartinfo ();
Process. filename = apppath;
Process. Arguments = "process" + I. tostring ();

Process. useshellexecute = false;
Process. createnowindow = true;

Process. redirectstandardoutput = true;
Process. Start (process );

// String result = P. standardoutput. readtoend ();
// Console. writeline ("the console app output is {0}", result );
Console. writeline ("process {0} start", I );
}

Static void waitsonprocess (int I)
{
Process = new process ();
String apppath = @ "E: \ vs2010code \ apptest \ showconsoleapp \ bin \ debug \ showconsoleapp.exe ";
Process. startinfo. filename = apppath;
Process. startinfo. Arguments = "process" + I. tostring ();

Process. startinfo. useshellexecute = false;
Process. startinfo. createnowindow = true;

Process. startinfo. redirectstandardoutput = true;

// Process. startinfo. windowstyle = processwindowstyle. normal;
// Start the process
Process. Start ();

Console. writeline ("process {0} start", I );
// Wait that the process exits
Process. waitforexit ();

Console. writeline ("the process had exits ");

// Now read the output of the DOS Application
String result = process. standardoutput. readtoend ();

Console. writeline ("the console app output is {0}", result );
}
}
}

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.