本文整理汇总了C#中System.Diagnostics.Process.Exited事件的典型用法代码示例。如果您正苦于以下问题:C# Process.Exited事件的具体用法?C# Process.Exited怎么用?C# Process.Exited使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.Diagnostics.Process
的用法示例。
在下文中一共展示了Process.Exited事件的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrintDoc
//引入命名空间
using System;
using System.Diagnostics;
using System.Threading.Tasks;
class PrintProcessClass
{
private Process myProcess;
private TaskCompletionSource<bool> eventHandled;
// Print a file with any known extension.
public async Task PrintDoc(string fileName)
{
eventHandled = new TaskCompletionSource<bool>();
using (myProcess = new Process())
{
try
{
// Start a process to print a file and raise an event when done.
myProcess.StartInfo.FileName = fileName;
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred trying to print \"{fileName}\":\n{ex.Message}");
return;
}
// Wait for Exited event, but not more than 30 seconds.
await Task.WhenAny(eventHandled.Task,Task.Delay(30000));
}
}
// Handle Exited event and display process information.
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
$"Exit time : {myProcess.ExitTime}\n" +
$"Exit code : {myProcess.ExitCode}\n" +
$"Elapsed time : {Math.Round((myProcess.ExitTime - myProcess.StartTime).TotalMilliseconds)}");
eventHandled.TrySetResult(true);
}
public static async Task Main(string[] args)
{
// Verify that an argument has been entered.
if (args.Length <= 0)
{
Console.WriteLine("Enter a file name.");
return;
}
// Create the process and print the document.
PrintProcessClass myPrintProcess = new PrintProcessClass();
await myPrintProcess.PrintDoc(args[0]);
}
}
示例2: ProcessDone
//引入命名空间
using System;
using System.Diagnostics;
public class DetectingProcessCompletion
{
static void ProcessDone(object sender, EventArgs e)
{
Console.WriteLine("Process Exited");
}
public static void Main()
{
Process p = new Process();
p.StartInfo.FileName = "notepad.exe";
p.StartInfo.Arguments = "process3.cs";
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(ProcessDone);
p.Start();
p.WaitForExit();
Console.WriteLine("Back from WaitForExit()");
}
}