我正在处理两个不同的应用程序。有没有办法在其他应用程序中的一个应用程序中捕获错误
我在Application 1 Solution中调用Application 2进程(.exe文件)。当应用程序2抛出一个“无效的用户名和密码”错误我想在应用程序1解决方案中捕获该错误异常。
有没有办法捕捉到错误在一个应用程序中的其他应用
我的应用程序是一个C#Windows应用程序
我正在处理两个不同的应用程序。有没有办法在其他应用程序中的一个应用程序中捕获错误
我在Application 1 Solution中调用Application 2进程(.exe文件)。当应用程序2抛出一个“无效的用户名和密码”错误我想在应用程序1解决方案中捕获该错误异常。
有没有办法捕捉到错误在一个应用程序中的其他应用
我的应用程序是一个C#Windows应用程序
进程边界之间你不能把渔获物。您必须使用进程间通信技术。根据您的情况,他们中的很多人可以选择。
下面是一些选项的列表...
使用文件或命名管道可能是最简单的方法去解决你想要解决的问题。其他选项取决于您的环境和要求。
加入约旦的解决方案,你可以读取控制台输出的Application2。
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
的......你可以做到这一点,如果你没有真正启动另一个过程,而是创建现有流程中的一个单独的AppDomain中运行可执行那种“内部“可执行文件也必须是CLR程序集。表明跨进程异常处理不可能的其他答案是正确的。这种方法只是解决这个问题,同时希望给你以后的行为。
刚开始的过程通常不会得到你想要的。
var process = Process.Start("MyInnerProcess.exe");
你会得到一个Process对象,为您提供各种有关过程多汁的信息,并允许您监视的事情被写入标准输出流......但例外没有真正进入内引发该过程。但是,如果你第一次运行一个新的AppDomain来启动你的程序集,你可以很容易地接收到该程序集在运行时抛出的所有异常(包括未处理和第一次机会!)的通知。 (注意:对于这些例外,你无能为力......但你会知道他们正在发生。)
下面的代码:
var innerDomain = AppDomain.CreateDomain("InnerDomain");
try
{
//Subscribe to the events you are interested in
innerDomain.UnhandledException += innerDomain_UnhandledException;
innerDomain.FirstChanceException += innerDomain_FirstChanceException;
//Execute your assembly within the app domain
innerDomain.ExecuteAssembly("MyInnerProcess.exe");
}
catch (Exception ex)
{
//Handle exceptions when attempting to launch the process itself.
}
void innerDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//Do something with the unhandled exceptions
}
void innerDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
//Do something with the first chance exceptions
}
现在,每当一个例外是其他进程抛出你会得到访问该例外。 FirstChanceException处理程序将针对每个异常(即使它在应用程序内正确处理)被调用,因此您可能不想订阅它。您可能最感兴趣的是UnhandledException事件。另外请记住,所有未处理的异常将首先触发FirstChanceException(当它们被引发时),然后触发UnhandledException,如果它们设法在没有被处理的情况下一直冒泡。
不错!真棒的第一篇文章。 – BenjaminPaul
这里的技巧将告诉我们应用程序1对于应用程序2中的错误所做的具体操作。您是否希望应用程序1使它像从未发生的错误一样,还是只是想记录错误。 –