0

I have a .net application written in c#. This application is called from a parent application written in c++. When the parent application crashes or accidently closes the .net application still runs. But I want to close the client application too when the parent application crashes or stops.

One of my colleague said we can easily implement this using a singleton class. How can I do that?

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
mystack
  • 4,910
  • 10
  • 44
  • 75

2 Answers2

1

You are going to have to poll to see if the parent application is still running.

One way to do this is setting up when an application was run by configuring audit process tracking in Windows. The following links might get you started:

Audit process tracking

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

The process tracking will create entries in the Windows event log which you can then access using C++, C# &/or VB.Net. You can use these logs to see if the Parent application is running.

Edit:

If you have access to the C++ codebase you could try to catch the exception when the parent app crashes or closes, eg:

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
}

Otherwise you will have to poll to see if the Parent application is listed in the Process Manager, eg:

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);
}

If its not listed, close the child application.

Community
  • 1
  • 1
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • But my issue is not with the parent aplication.How can i stop my client application when the parant crashes or stops. – mystack Apr 23 '13 at 05:30
  • no ihave no access to the c++ code base.But i have client application code base.Is it possiblr to manage it from client application using a singleton class – mystack Apr 23 '13 at 08:53
  • No, whoever said you can do it with a singleton (that can work across AppDomains & Processes) is smoking something funny. – Jeremy Thompson Apr 24 '13 at 06:58
0

Firstly, you have to catch event when the parent windows raise exception or closing. Secondly, you will send to close message to process which already launched by parent window. For your information about send close message you can take reference here: Send message to a Windows process (not its main window)

Or

how to close a window from a different process

Community
  • 1
  • 1
Toan Vo
  • 1,270
  • 9
  • 19