[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

IN VB6 TO FORCE CLOSING A "HANGING" PROGRAM

It happens that a "VB6 program is not properly closed by user, then it will be when I popup taskmanager still in "running tasks" I have to manualy force by clicking few times in taskmanager to get rid off it.
So I wonder would there be a way to do this from VB6 from my startup VB6 program from where I shell to all the other VB6 programs
0
BIAPRO
Asked:
BIAPRO
  • 4
  • 4
1 Solution
 
Antagony1960Commented:
If your program is not shutting down fully it is because there is a process running which isn't being unloaded by the main form's unload event. You should really put code in there to make sure any hidden forms and processes are unloaded in an orderly fashion. Of course a simple End statement will accomplish pretty much the same thing.

Failing that, this code should accomplish what you want:

Public Function EndProcess(sProcessName As String) As Boolean
Dim winProcess
    For Each winProcess In GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Process")
        'Debug.Print winProcess.Name
        If LCase(winProcess.Name) = LCase(sProcessName) Then
            winProcess.Terminate
            EndProcess = True
            Exit For
        End If
    Next
End Function

Open in new window

0
 
Antagony1960Commented:
Oh, and I should point out that that EndProcess function will not work on programs running from the IDE, only on the executables.
0
 
BIAPROAuthor Commented:
ok, what would be the syntax to use this function at the end off my program   ( private sub form_unload part)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Antagony1960Commented:
I take it you want to close all your managed programs when the startup application gets closed--they are separate programs (executables) aren't they? If so, you call the function like this:

EndProcess "myProgram1.exe"

Alternatively, as it is a boolean function, you could use its return value to log whether it has succeeded or failed, like this:

If EndProcess("myProgram1.exe") Then
    'Unloaded successfully'
Else
    'Failed to unload, may already have been closed'
End If
0
 
BIAPROAuthor Commented:
Thanks
0
 
BIAPROAuthor Commented:
1 qst, is it possible to find at running time the programname, myProgram1.exe  instead off fixed text?
0
 
Antagony1960Commented:
Hmm, the function would need to know how to distinguish between your programs and any others in the process list.

Are the programs you want to unload located in the same folder, where no other programs (which you wouldn't want to close) reside? If so you could use the ExecutablePath property of the winProcess object to check whether found processes should be closed. Try the sub below. (You will need to add a program reference to the "Microsoft Scripting Runtime")

Public Sub EndMyApps()
Const sAppFolder As String = "C:\MyAppFolder"
Dim winProcess, FSO As New FileSystemObject, sExeFolder As String
    For Each winProcess In GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Process")
        sExeFolder = FSO.GetParentFolderName("" & winProcess.ExecutablePath)
        'Terminate any programs (except this one) running from the app folder'
        If sExeFolder = sAppFolder Then
            If winProcess.Name <> App.EXEName & ".exe" Then winProcess.Terminate
        End If
    Next
    Set FSO = Nothing
End Sub

Open in new window

0
 
BIAPROAuthor Commented:
True , allways in same drive/folder (network), so thanks again
Regards
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now