Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I make it so that only one instance of my application can run?

Status
Not open for further replies.

LeonKl2

Programmer
Jun 16, 2003
26
US
I need to make sure that the user can't start 2 or more copies of my program at the same time.

How do I do this?

Or alternatively, is there any way for a running program to be able to read info from the command line?

So instead of it opening another copy of my program with this command line options, it would just send this information to the already running program.

Thanks
 
Hi LeonKl2,

And if you wish to have the command line from the second copy of the program to be available to the first copy, you can to this using a DDEConversation. Try the following:

Create a new project and add a textbox called "txtCommand" to the form. Change the form LinkMode property to 1 - Source.

Then paste the following into the code window for the form

Code:
Option Explicit

Dim bProcessTextChange As Boolean

Private Sub Form_Load()
    
    With Form1
        .LinkTopic = App.EXEName ' can be any text you wish
                                 ' just needs to be common to both
                                 ' copies of the program
    End With
    
    If App.PrevInstance Then
        bProcessTextChange = False  ' so that setting txtcommand.text
                                    ' in the copy of the program does
                                    ' not trigger your code in the
                                    ' txtCommand_Change event in the
                                    ' copy
        With txtCommand
            Form1.LinkMode = vbLinkNone
            .Text = Command
            .LinkTopic = App.Title & "|" & Form1.LinkTopic
            .LinkItem = "txtcommand" ' must be the name of
                                     ' the textbox to
                                     ' receive the command
                                     ' line
            .LinkMode = vbLinkManual
            .LinkPoke
            .LinkMode = vbLinkNone
        End With
        Unload Me
    Else
        bProcessTextChange = True
    End If
    
End Sub

Private Sub txtCommand_Change()
    If bProcessTextChange = False Then
        ' change event triggered by the copy of the program
        Exit Sub
    End If
        
    ' Insert your code here to process the command line
    
End Sub

I am sure that there will be other (more elegant?) ways that you can achieve this result.



Hope this helps ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top