SoFunction
Updated on 2025-03-07

Summary of several common methods for starting external programs in C#

This article summarizes several common methods for starting external programs in C#, which are very practical and mainly includes the following methods:

1. Start an external program without waiting for it to exit.
2. Start an external program and wait for it to exit.
3. Start an external program and wait for it to exit infinitely.
4. Start an external program and monitor it to exit through events.

The implementation code is as follows:

// using ;
private string appName = "";
/// <summary>
/// 1. Start an external program without waiting for it to exit/// </summary>
private void button1_Click(object sender, EventArgs e)
{
(appName);
(("External Programs {0} Startup is complete!", ), ,
, );
}
/// <summary>
/// 2. Start an external program and wait for it to exit/// </summary>
private void button2_Click(object sender, EventArgs e)
{
try
{
Process proc = (appName);
if (proc != null)
{
(3000);
if () (("External Programs {0} Quitted!", ), ,
, );
else
{
// If the external program does not end, it will be terminated by force.();
(("External Programs {0} Forced terminated!", ), , , );
}
}
}
catch (ArgumentException ex)
{
(, , , );
}
}
/// <summary>
/// 3. Start an external program and wait for it to exit infinitely/// </summary>
private void button3_Click(object sender, EventArgs e)
{
try
{
Process proc = (appName);
if (proc != null)
{
();
(("External Programs {0} Quitted!", ), ,
, );
}
}
catch (ArgumentException ex)
{
(, , , );
}
}
/// <summary>
/// 4. Start an external program and monitor its exit through events/// </summary>
private void button4_Click(object sender, EventArgs e)
{
try
{
//Start external programsProcess proc = (appName);
if (proc != null)
{
//The monitoring process exits = true;
//Specify the exit event method += new EventHandler(proc_Exited);
}
}
catch (ArgumentException ex)
{
(, , , );
}
}
/// <summary>
/// Start the external program exit event/// </summary>
void proc_Exited(object sender, EventArgs e)
{
(("External Programs {0} Quitted!", ), ,
, );

Readers can choose the methods in this example according to the situation, hoping that they can be of some help to learn from everyone's C# programming.