2013-04-23 61 views
0

我有用c#编写的.net应用程序。此应用程序从用C++编写的父应用程序调用。当父应用程序崩溃或意外关闭.net应用程序仍在运行。但是我想在父应用程序崩溃或停止时也关闭客户端应用程序。使用Singleton类实现关闭客户端应用程序

我的一位同事说我们可以很容易地实现这个使用单例类。我怎样才能做到这一点?

回答

1

您将不得不轮询以查看父应用程序是否仍在运行。

通过在Windows中配置审计进程跟踪来运行应用程序时,可以设置一种方法。下面的链接可能让你开始:

Audit process tracking

How can I track what programs come and go on my machine?

过程跟踪将创建在Windows事件日志条目,然后您可以访问使用C++,C#& /或VB 。净。您可以使用这些日志来查看父应用程序是否正在运行。

编辑:

如果你有机会到C++代码库,你可以尝试捕捉异常的父应用程序崩溃或关闭时,如:

Application.ThreadException += new ThreadExceptionEventHandler(ApplicationThreadException); 

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ApplicationUnhandledException); 


private void ApplicationThreadException(object sender, ThreadExceptionEventArgs e) 
{ 
    //Write to a File, Registry, Database flagging the application has crashed 
} 
private void ApplicationUnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    //Write to a File, Registry, Database flagging the application has crashed 
} 

private void frmMain_FormClosed(object sender, FormClosedEventArgs e) 
{ 
    //Write to a File, Registry, Database flagging the application has closed 
} 

否则,你将不得不轮询看如果父应用程序在过程管理器中列出,例如:

StringBuilder sb = new StringBuilder(); 
ManagementClass MgmtClass = new ManagementClass("Win32_Process"); 

foreach (ManagementObject mo in MgmtClass.GetInstances()) 
{ 
    sb.Append("Name:\t" + mo["Name"] + Environment.NewLine); 
    sb.Append("ID:\t" + mo["ProcessId"] + Environment.NewLine); 
    sb.Append(Environment.NewLine); 
} 

如果未列出,请关闭子应用程序。

+0

但我的问题是不是与父母aplication.How我能停止我的客户端应用程序时parant崩溃或停止。 – TVSuser1654136 2013-04-23 05:30:50

+0

看我的编辑.... – 2013-04-23 06:05:36

+0

没有ihave没有访问的c + +代码base.But我有客户端应用程序代码base.Is possiblelr管理它从客户端应用程序使用singleton类 – TVSuser1654136 2013-04-23 08:53:48