好像进程退出处理可能造成应用程序错误。所以应用程序可能已经终止。你可以把一个适当的try..catch块和调试,看看有什么问题。或注释 线
newCrawler.Exited += new EventHandler(newCrawler_Exited);
and see what happens.
请尝试以下代码(这是从MSDN),也不要忘了传递一个参数(文件名)
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
using Microsoft.VisualBasic;
class PrintProcessClass
{
private Process myProcess = new Process();
private int elapsedTime;
private bool eventHandled;
// Print a file with any known extension.
public void PrintDoc(string fileName)
{
elapsedTime = 0;
eventHandled = false;
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 \"{0}\":" + "\n" + ex.Message, fileName);
return;
}
// Wait for Exited event, but not more than 30 seconds.
const int SLEEP_AMOUNT = 100;
while (!eventHandled)
{
elapsedTime += SLEEP_AMOUNT;
if (elapsedTime > 30000)
{
break;
}
Thread.Sleep(SLEEP_AMOUNT);
}
}
// Handle Exited event and display process information.
private void myProcess_Exited(object sender, System.EventArgs e)
{
eventHandled = true;
Console.WriteLine("Exit time: {0}\r\n" +
"Exit code: {1}\r\nElapsed time: {2}", myProcess.ExitTime, myProcess.ExitCode, elapsedTime);
}
public static void 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();
myPrintProcess.PrintDoc(args[0]);
}
}
有一件事我就是注意到了,如果你没有传递文件名作为参数,这会导致进程崩溃,但应用程序仍然是完整的(因为异常在进程内部处理)。
如果您没有传递文件名,上面的代码会崩溃,因为myPrintProcess.PrintDoc(args [0])是 ; 会从主进程本身抛出异常。 我试图在Exit处理程序中创建一个exceptin,那时应用程序(主进程)也崩溃了。
你可以尝试评论退出处理程序内的代码吗?
谢谢,但我需要使用退出事件,因为我不想阻止该应用程序,直到进程存在 – Ehsan 2011-01-14 09:45:39
好,但谁是保持主处理(控制台应用程序)仍在运行?你说它退出... – 2011-01-14 09:47:01