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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tommy_boyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.