How to close .exe applications opened by a master exe.

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi. I have a large project which will lead to a large exe (in terms of file size). So I am thinking of having an exe consisting of a LogInAndWhereToFromHere selection of buttons which run the other exe files, eg. CustomersAddresses.exe, Invoicing.exe, Reports.exe etc. All in VB.Net.

Opening these exe files will be easy, but if one or two of these other exe files are open when the user closes LogInAndWhereToFromHere.exe, how can I get the other exe files to close automatically?

Thank you.
 

Attachments

  • LogInAndWhereToFromHere.png
    LogInAndWhereToFromHere.png
    30.4 KB · Views: 31
Use the Form Closing or Closed events to close active processes. Are you absolutely sure you want to do this though. Some users will inevitably assume that they can choose the programs they want and then close the 'menu' before working with them.
 
One way you can do this is when the user clicks a button and you run the other program, keep a reference to the instance of that other program, hide the form and use the process's WaitForExit() to know when the user has exited the other app, you can then re-show your form here so they can exit your "suit" of apps altogether or run a different program from it.

This way you can keep this "master" program running, but hidden when the user is running the sub-app and have it still be independent of these sub-apps.
 
If you really want to do as you suggest then you should manage a child program something like this:
Private WithEvents childProcess As Process

Private Sub StartChildProcess()
    If childProcess Is Nothing Then
        childProcess = Process.Start(IO.Path.Combine(Application.StartupPath, "ChildProgram.exe"))
    End If
End Sub

Private Sub EndChildProcess()
    If childProcess IsNot Nothing Then
        If childProcess.CloseMainWindow() Then
            'Wait up to 10 seconds for the child process to close.
            childProcess.WaitForExit(10000)
        End If

        If Not childProcess.HasExited Then
            'Forcibly end the process.
            childProcess.Kill()
        End If

        childProcess = Nothing
    End If
End Sub

Private Sub childProcess_Exited(sender As Object, e As System.EventArgs) Handles childProcess.Exited
    childProcess = Nothing
End Sub
That said, I would recommend against multiple executables. If you want to break up the functionality then create a DLL for each component and have the main application reference them. That will keep the EXE relatively small but still allow you to manage everything as a single application. Along with reuse, that's exactly what DLLs are for.
 
I created a dll project with one form. Added it as a Reference. Seems OK. However, when debugging the dll project I added a second form, but the first form won't reference the second, ie. frmTest2.Show isn't available - so the user can't open it. Is this a limitation of dll projects?
 
The user can open it. You just have to create an instance yourself rather than use the default instance. Most experienced VB.NET, myself included, would tell you not to use a default instance anyway. If you want to learn a bit more about default instances then follow the Blog link in my signature and read my post on the subject. Default instances are provided via My.Forms, which doesn't exist except in a Windows Forms Application project.
 
I created a dll project with one form. Added it as a Reference. Seems OK. However, when debugging the dll project I added a second form, but the first form won't reference the second, ie. frmTest2.Show isn't available - so the user can't open it. Is this a limitation of dll projects?

The user can open it. You just have to create an instance yourself rather than use the default instance. Most experienced VB.NET, myself included, would tell you not to use a default instance anyway. If you want to learn a bit more about default instances then follow the Blog link in my signature and read my post on the subject. Default instances are provided via My.Forms, which doesn't exist except in a Windows Forms Application project.

As jmc has pointed out you're trying to use a default instance of the form, which people really shouldn't be in the habit of using those.

Just my 2 cents, MS shouldn't have added default instances to .Net back in 2005 in the first place.
 
Back
Top