I have a couple of applications that depend on each other. One is a robust vb6 application. The other is my eagerly anticipated, stealth technology employing, and likely oscar winning .net (2.0) systray application. The vb6 app starts the systray app. And if the VB6 app is shut down, the systray app will shut itself down assuming it has no other work to do (it's work is saving documents to a database).
My sole remaining concern is what happens if the systray app crashes or cannot do its work due to some fatal error. A likely outcome of this condition is me popping up a modal dialog telling the user "contact support immediately, because you can'开发者_Python百科t save documents any more".
I think the worst case scenario here is that whenever a document is processed in the vb6 app (that is, a user completes the document and it's components are zipped up and saved locally), I'll just have to check and see if the systray app is running. I don't know, that seems a bit cumbersome. Is there a more elegant approach? Is there some way I can "signal" my vb6 app/have my vb6 app listen for such a signal?
what I have done in the past is a class library in .NET exposed as COM object (in the assembly info, put ComVisible attribute to true).
This way from VB6 you can call the methods you have exported from .NET class library and in C# you can have all the code you need to handle the situation.
In this way I write less code in VB6 and more in C# where we can profit of better exception handling (try-catch-finally), logging frameworks like Log4Net and so on...
this has worked very well for me so far.
You could use WMI to check if it's running quite easily by using code similar to:
dim foundIt
For Each Process in GetObject( "winmgmts://." ).InstancesOf( "win32_process" )
If UCase( Process.name ) = "MYDOTNET.EXE" Then
foundIt = true
End If
Next
if not foundIt then
msgbox( "It's not running, so start the process")
end if
Please note, wrote the above as VBscript, but should work as VB6 code I would have thought.
I don't have details handy but I've used windows messaging to send a WM_NULL in the past. It amounts to a ping message. So the VB6 app sends a periodic ping to the systray app. If the systray app doesn't respond to the ping in a timely manner then the VB6 app knows it is hung or not running.
My first guess would be introduce intercommunication between the two. Have the VB6 app send a PING request on a defined interval. Then if the VB6 app does not receive a response or the request times out, do what you need to do.
I would suggest using something like the following function on a timer:
'declaration for FindWindow API
Private Declare Function FindWindow _
Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'Call the following on a timer with the Tray icon form caption
'when it goes False unleash hell
Private Function IsWindowLoaded(ByVal strWindowCaption As String) As Boolean
Dim lHwnd As Long
lHwnd = FindWindow(strWindowCaption, vbNullString)
If lHwnd <> 0 Then
IsWindowLoaded = True
Else
IsWindowLoaded = False
End If
End Function
精彩评论