How do I check if a VB6 application is running (and not hung on a system error) from C#?
I need to monitor a legacy application written in vb6 from a C# service and force-quit/relaunch it whenever it hangs.
How do I check i开发者_JS百科f a VB6 application is running (and not either hung on a system error or crashed completely) from C#?
Thanks!
You can easily use the Process
type to monitor for crashes.
However, it's tough to tell if an app is 'temporarily busy' or actually 'hung', so we will need to make a few assumptions.
Lets say that this is a windows forms app (in VB6) that has an active message pump.
Lets also say that a timeout of 10 seconds is a good indication of a 'hung' app.
Any function that relies on that message pump to complete will be a good indicator of a hung app, given that it exceeds the timeout.
You can use any of the SendMessage
commands to basically 'join' with the other app's message pump.
Something like this:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
...
NativeMethods.SendMessage(hWnd, NativeMethods.WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
精彩评论