CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2002
    Location
    Colorado
    Posts
    105

    Killing Previous Instance of VB6 Application

    I've searched around for the proper way to kill a previous Instance of a VB6 application. My situation seems a bit unique. If our main sevice(outside of the application) dies, we need to terminate the previous instance of the running application and then, when the service comes back up only the new instance should be loaded. I am trying to exit this code in the following function:

    Code:
    Private Sub g_cServerInterface_FatalError(Error As enSvrReturns, ErrorString As String)
    
        Dim sMsg As String
        Dim result As VbMsgBoxResult
        
        m_bFatalError = True
    
        UnFreeze
        
        If m_cLanguageText Is Nothing Then
            GoTo TheEnd    'Form not yet loaded - not yet logged on
        End If
        
    '    m_NumFatalErrors = m_NumFatalErrors + 1
    '    If m_NumFatalErrors > 5 Then
    '        Functions.DevInfo "Unable to restart Manitou.", g_cLangText_General
    '        End
    '    End If
        
        If Error <> SVRERR_NOT_CONNECTED Or RunningInDebugger() Then
            sMsg = g_cLangText_General.GetText("A system error has occurred")
            
            If ErrorString <> "" Then
                sMsg = sMsg & ":" & vbCrLf & vbCrLf & ErrorString & vbCrLf & vbCrLf
            Else
                sMsg = sMsg & ".  "
            End If
            
            sMsg = sMsg & g_cLangText_General.GetText("Press OK to attempt to restart or Cancel to quit.")
            
            result = DevAskOkCancel(sMsg, Nothing)
        Else
            ' Since we've been disconnected, attempt immediately to reconnect
            result = vbOK
        End If
            
        If (result = vbOK) Then
            On Local Error Resume Next
            If InStr(g_CommandLine, "-U") = 0 Then
                g_CommandLine = g_CommandLine & " -U" & g_cUser.id
            End If
            If InStr(g_CommandLine, "-P") = 0 Then
                g_CommandLine = g_CommandLine & " -P" & g_cUser.Password
            End If
            Shell App.Path & "\" & App.EXEName & " " & g_CommandLine & " -X", vbNormalFocus
            DoEvents
        End If
        
    TheEnd:
        If (Not RunningInDebugger()) Then
            ' Running as compiled executable
            ' Specifies the exit code for the process, and for all threads that
            ' are terminated as a result of this call. Use the GetExitCodeProcess
            ' function to retrieve the process's exit value. Use the GetExitCodeThread
            ' function to retrieve a thread's exit value.
            
            CoUninitialize
            ExitProcess 0
        Else
            ' Running from the IDE
            End
        End If
    
    End Sub

    Note that I added the CoUninitialize and ExitProcess 0 API calls to this. How can I correctly terminate the perviously loaded instance when the service comes back up? thanks Larry
    Last edited by lbargers; March 4th, 2013 at 09:15 AM.

  2. #2
    Join Date
    Apr 2012
    Posts
    43

    Re: Killing Previous Instance of VB6 Application

    Is there some reason you cannot have the application terminate itself ?

  3. #3
    Join Date
    Mar 2002
    Location
    Colorado
    Posts
    105

    Re: Killing Previous Instance of VB6 Application

    The instance I described above is a for a condition that could occur when another service that is checking licensing of the product goes down... if that service is disabled or crashes, then the connected VB Application should also go back to the dlgLogon.frm and wait for the service to come back online. When the other service comes back online this will keep multiple instance of the VB Application open. We want to terminate the previous running instance so only one VB6 Application remains running.

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Killing Previous Instance of VB6 Application

    Why not use a FLAG to tell the program when the service is UP? You can restart the program at that point...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured