none
how to run multiple exe from the code RRS feed

  • Question

  • how to run multiple exe from the code

    Module ModMain
        Private m_Handler As New SingleInstanceHandler()
        Private m_MainForm As SingleInstanceForm
    
        Public Sub Main(ByVal args() As String)
            AddHandler m_Handler.StartUpEvent, AddressOf StartUp ' Add the StartUp callback
            m_Handler.Run(args)
        End Sub
    
        Public Sub StartUp(ByVal sender As Object, ByVal event_args As StartUpEventArgs)
            If event_args.NewInstance Then ' This is the first instance, create the main form and addd the child forms
                m_MainForm = New SingleInstanceForm()
                m_MainForm.AddChildForms(event_args.Args)
                Application.Run(m_MainForm)
            Else ' This is coming from another instance, so simple add the child forms
                ' We are probably calling from a seperate thread, so we need to use the forms Invoke command
                ' to ensure that we are perform the AddForm method in the proper thread.
                Dim parameters() As Object = {event_args.Args}
                m_MainForm.Invoke(CType(AddressOf m_MainForm.AddChildForms, SingleInstanceForm.AddChildFormsDelegate), parameters)
            End If
        End Sub
    
    End Module
    Public Class SingleInstanceHandler
        Inherits MarshalByRefObject
    
        Private m_Mutex As System.Threading.Mutex
        Private m_UniqueIdentifier As String
        Private m_TCPChannel As System.Runtime.Remoting.Channels.Tcp.TcpChannel
    
        Public Sub Run(ByVal args() As String)
            ' Create a Unique Identifier for the Mutex object and the SingletonCommunicator
            ' We are using the executable path for the application because we want to ensure
            ' that the instances are unique based on installation locations.
            ' Hack: Attempting to create a Mutex with an identifier that looks like a path
            '       will cause a path not found exception. So simply replace the '\'s with
            '       underscores and this should still be a unique identifier for this app
            '       based on the running location.
            m_UniqueIdentifier = Application.ExecutablePath.Replace("\", "_")
    
            ' Create the Mutex class
            m_Mutex = New System.Threading.Mutex(False, m_UniqueIdentifier)
            ' Attempt to lock the Mutex
            If m_Mutex.WaitOne(1, True) Then ' We locked it! We are the first instance!!!
                CreateInstanceChannel()
                ' Raise the StartUpEvent as a NewInstance
                Dim event_args As New StartUpEventArgs(True, args)
                RaiseStartUpEvent(event_args)
            Else ' Not the first instance!!!
                ' Raise the StartUpEvent through the SingletonCommunicator not as a NewInstance
                Dim event_args As New StartUpEventArgs(False, args)
                UseInstanceChannel(event_args)
            End If
        End Sub
    
        Private Sub CreateInstanceChannel()
            System.Runtime.Remoting.RemotingServices.Marshal(Me, m_UniqueIdentifier)
            Dim tcp_properties As IDictionary = New Hashtable(2)
            tcp_properties("bindTo") = "127.0.0.1"
            tcp_properties("port") = 0
            m_TCPChannel = New System.Runtime.Remoting.Channels.Tcp.TcpChannel(tcp_properties, Nothing, Nothing)
            System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(m_TCPChannel)
            Dim key As Microsoft.Win32.RegistryKey = Application.UserAppDataRegistry()
            key.SetValue(m_UniqueIdentifier, m_TCPChannel.GetUrlsForUri(m_UniqueIdentifier)(0))
        End Sub
    
        Private Sub UseInstanceChannel(ByVal event_args As StartUpEventArgs)
            Dim key As Microsoft.Win32.RegistryKey = Application.UserAppDataRegistry()
            Dim remote_component As SingleInstanceHandler = CType(System.Runtime.Remoting.RemotingServices.Connect(GetType(SingleInstanceHandler), _
                                                            key.GetValue(m_UniqueIdentifier)), SingleInstanceHandler)
            remote_component.RaiseStartUpEvent(event_args)
        End Sub
    
        Public Sub RaiseStartUpEvent(ByVal event_args As StartUpEventArgs)
            RaiseEvent StartUpEvent(Me, event_args)
        End Sub
    
        Public Event StartUpEvent As StartUpEventArgsHandler
    End Class
    
    <Serializable()> _
    Public Class StartUpEventArgs
        Inherits EventArgs
        Public Sub New(ByVal new_instance As Boolean, ByVal the_args() As String)
            NewInstance = new_instance
            Args = the_args
        End Sub
    
        Public NewInstance As Boolean
        Public Args() As String
    End Class
    
    Public Delegate Sub StartUpEventArgsHandler(ByVal sender As Object, ByVal event_args As StartUpEventArgs)

    https://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET

    How to open another program such as Calculator



    Tuesday, February 27, 2018 1:46 AM

All replies



  • How to open another program such as Calculator



    In order to open another program in VB.NET, please use Process.Start(<executable file name>).
    Process.Start("calc.exe")
    Regards,

    Ashidacchi

    • Edited by Ashidacchi Tuesday, February 27, 2018 11:26 PM
    Tuesday, February 27, 2018 4:31 AM
  • Brother I want how to call the executable to open more than once inside in form

    like this    

    https://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET

    Tuesday, February 27, 2018 10:18 PM
  • Hi,

    I'm afraid you are using wrong words "the executable" and "program". They should be "Form". Or your are using wrong example. Doesn't that URL help you? 

    I can hardly understand what you want to achieve.

    If you have some trouble in your code, please specify line which causes the issue.

    Regards,


    Ashidacchi

    • Edited by Ashidacchi Tuesday, February 27, 2018 10:35 PM
    Tuesday, February 27, 2018 10:28 PM
  • Brother I want how to call the executable to open more than once inside in form

    like this    

    https://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET

    Do you realize how outdated that codeproject article is?  For a single-instance application you simply double click My Project in the solution explorer, go to the Application tab, and check the Make single instance application checkbox.  There is no code required at all.

    If you want to handle the StartupNextInstance event, click the Application Events button on that page and code an event handler in the MyApplication class.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, February 27, 2018 10:34 PM
    Moderator
  • Brother I want how to call the executable to open more than once inside in form

    There is no obvious connection between https://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET and "How to open another program such as Calculator".  You need to provide much more detail about what you are trying to do.

    Tuesday, February 27, 2018 10:39 PM
  • Hi,

    Do you want to make MDI (Multi-Document Interface) application?
    If so, please search web using "MDI". One of them is 
    Multiple-Document Interface (MDI) Applications

    Regards,  

    Ashidacchi

    Tuesday, February 27, 2018 11:33 PM