Link to home
Start Free TrialLog in
Avatar of emi_sastra
emi_sastra

asked on

MENU FOR SOME EXE

Hi All,

I have several separate applications (exe).
Now, I want to put it into a separate application that shows those exe to select.

How could I do it ?

Thank you.
Avatar of Misbah
Misbah
Flag of United States of America image

let us assume you have three applications,
in the menu, add three items (app1, app2, app3)

when the user select a menu item, run the application

in the menu_selection_changed event , write a code like this:

if  (menuItem = "app1")
System.Diagnostics.Process.Start("C:\app1.exe")

else if (menuItem = "app2")
System.Diagnostics.Process.Start("C:\app2.exe")


is this solution clear ?
Avatar of emi_sastra
emi_sastra

ASKER

Hi  Dr-Hussain,

Show I use mdi form or just a form with several buttons.
Since the main menu for application is simple.

How if the user select the opened application ?
How to check if the application is running and ask user to choose to open another one or activate the current running instance ?

Thank you.
use p1.HasExited to check that, see the code below:

  ProcessStartInfo pinfo1 = new ProcessStartInfo();
  pinfo1.FileName = "c:\\app1.exe";


            Process p1 = Process.Start(pinfo1);
            p1.WaitForExit();  // if you use this, then the user will be allowed only to interact with app1.exe and after he finish then he can interact with the main application to run other apps .

// you can also use 
          if(  p1.HasExited )
{
 // app1 was closed
}
else
{
// app1 is still running
}

Open in new window

Below is my code :

Public Class frmMain

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click

        Dim strApplicationName As String = Application.StartupPath & "\" & "ERV_INVENTORY.EXE"

        Process.Start(strApplicationName)
    End Sub

    Private Sub btnFinance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinance.Click

    End Sub

    Private Sub btnGeneralLedger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGeneralLedger.Click

    End Sub
End Class


Where to put your code  ?

Thank you.
Your code in vb :

Dim pinfo1 As New ProcessStartInfo()
pinfo1.FileName = "c:\app1.exe"


Dim p1 As Process = Process.Start(pinfo1)
p1.WaitForExit()
' if you use this, then the user will be allowed only to interact with app1.exe and after he finish then he can interact with the main application to run other apps .
' you can also use
            ' app1 was closed
If p1.HasExited Then
            ' app1 is still running
Else
End If

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi  Idle_Mind,

Please see below code,  ps.Length always return 0 ?

Option Explicit On

Public Class frmMain

    Dim strApplicationName As String = ""
    Dim strAppName As String = ""

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click

        Open_Application("ERV_INVENTORY.BAT")

    End Sub

    Private Sub btnFinance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinance.Click
        Open_Application("ERV_FINANCE.BAT")
    End Sub

    Private Sub btnGeneralLedger_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGeneralLedger.Click
        Open_Application("ERV_GL.BAT")
    End Sub

    Private Sub Open_Application(ByVal strAppBatchFileName As String)

        strApplicationName = Application.StartupPath & "\" & strAppBatchFileName
        strAppName = Replace(strApplicationName, ".BAT", ".EXE")

        Try

            If Me.Is_Application_Running(strAppName) Then
                Dim ps() As Process = Process.GetProcessesByName(strAppName)

                If MessageBox.Show("Activate the previous instance?", "Application is already running.", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                    AppActivate(ps(0).Id)
                Else
                    Dim p As New Process()

                    p.EnableRaisingEvents = False
                    p.StartInfo.UseShellExecute = False
                    p.StartInfo.CreateNoWindow = True
                    p.StartInfo.FileName = strApplicationName
                    p.Start()
                End If
            Else
                Dim p As New Process()

                p.EnableRaisingEvents = False
                p.StartInfo.UseShellExecute = False
                p.StartInfo.CreateNoWindow = True
                p.StartInfo.FileName = strApplicationName
                p.Start()

            End If

        Catch ex As Exception
            MsgBox(ex.ToString)
            MsgBox("Error Opening Application ...!", MsgBoxStyle.Information, Me.Text)
        End Try

    End Sub

    Private Sub btn_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
                btnInventory.MouseHover, _
                btnFinance.MouseHover, _
                btnGeneralLedger.MouseHover

        Me.Cursor = Cursors.Hand

    End Sub


    Private Sub btn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
                btnInventory.MouseLeave, _
                btnFinance.MouseLeave, _
                btnGeneralLedger.MouseLeave

        Me.Cursor = Cursors.Default

    End Sub

    Private Function Is_Application_Running(ByVal strProcessName As String) As Boolean

        Try

            Dim ps() As Process = Process.GetProcessesByName(strProcessName)

            If ps.Length = 0 Then Return False

            Return True

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

        Return False

    End Function
 
End Class

What's the problem ?

Thank you.
I get it.

Function Is_Application_Running :

   Dim ps() As Process = Process.GetProcessesByName(strProcessName)

should be :

 Dim ps() As Process = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(strProcessName))

Thank you.
Great.

Thank you very much for your help.
Exactly...was just about to point that out.  =)