One instance

Is there a way to ensure that only one instance of my VB6 application is running on a computer at one time?  Or, is there a way to lock a database so that only one application can read or write to it at one time?
rosscAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
tommy_boyConnect With a Mentor Commented:
Detecting Previous Instances

Function AnotherInstance () As Integer
    Dim AppTitle$
    If App.PrevInstance Then
        ' Hold the title of the application (title bar caption)
        AppTitle$ = App.Title  
        ' Change our application title
        App.Title = "No longer want this app running..."
        ' Activate the previous instance
        AppActivate AppTitle$  
        ' Let calling procedure know another instance was detected
        AnotherInstance = True  
    Else
        ' Let calling procedure know another instance was NOT detected
        AnotherInstance = False
    End If
End Function

Sub Form_Load ()
    ' Don't want two copies of the program running at the same time
    If AnotherInstance() Then End
    ' Note: that this routine will not work if the application's title changes
    ' (showing file names in the title bar for example).
End Sub
0
 
mark2150Commented:
in the Form_Load:

If App.PrevInstance then END

M
0
 
tommy_boyCommented:
..and that is what is in my code mark2150
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mark2150Commented:
You do it in a function call and a dozen lines of code. I do it in one line.

Which is clearer in intent?
Which is less compiled bytes?
Which is faster to execute?
Which has fewer lines to debug?
Which took less time (read cost) to code?

Professional coding is price driven. I'm not going to pay one of my team to write like that when the one line version is simpler and clearer.

Had a guy write something like this:

TempTxt = Textbox.Text
If Len(Trim(TempTxt)) = 0 Then
    Str1 = Str1 & " This is a prompt: " & Trim(TempText) & VbCrLf
Else
    Str1 = Str1 & " This is a prompt: " & VbCrLf
End If

Now if you look at that you'll find it'll simplify to:

Str1 = Str1 & " This is a prompt: " & Trim(TextBox.Text) & VbCrLf

Five fewer lines, one less working variable faster execution, much clearer intent. Need I say that this person is no longer working for us...

M

0
 
tommy_boyCommented:
so mark2150, no validation, no activeating pervious instance, yeah, that is good.
0
 
mark2150Commented:
Depends on if you want that or no. Certainly there are times that is good and wanted, but Q wasn't how to bring up alternate, but how to prevent any additional copies from running.

Your change to titlebar will be followed almost instantly by END so user will *NEVER SEE IT*. Ergo: What is point?

Activating alternate may or may not work as you pointed out yourself!

Finally, what is point of coding function call that is only going to be used *once*? Good programming practice says that "single call" functions or subs should be *inlined* for speed and clarity.

M
0
 
rosscAuthor Commented:
Thanks for the help.
0
All Courses

From novice to tech pro — start learning today.