I have my app built but i can open more than one at a time. I need to know how to stop the user from opening the app more than once. Thanks
Printable View
I have my app built but i can open more than one at a time. I need to know how to stop the user from opening the app more than once. Thanks
This is a pretty easy one.. in the loading event of the program (either Form_Load of your first form or the Sub Main() event).. put these lines..
That should solve your problem!Code:' If another application is running then end the program.
If App.Previnstance = True then
MsgBox "Another instance of this program is already running!"
End
End if
- Aurilus
Thanks. Works like a charm. I have never needed to do it so i didnt know.
Thanks again.
If you want to do it through the API... you could always create a named Mutex.
That should workPHP Code:Public Const MAX_PATH = 260
Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Public Const ERROR_ALREADY_EXISTS = 183&
Dim MutexName as String * MAX_PATH
Function TestIfRunning(MutexName as String) As Boolean
if CreateMutex(0,true,MutexName) <> ERROR_ALREADY_EXISTS Then
TestIfRunning=false
else
TestIfRunning=true
End If
End Function
But why do it through the API? The performance gain would be negligible..
I don't know much about this particular API call, but would the Mutex be destroyed when the program ends automatically, or would another API call have to be made to destroy it?
- Aurilus
> But why do it through the API?
If you have to ask, then you probably wouldn't understand.
;) JK.
:mad: <sarcasm>Thanks JK for your answer. It really helped!</sarcasm> :mad:
I do use a lot of API calls.. for a wide range of things ranging from getting registry keys, setting windows positions, bit blitting, etc. and I have a good grasp of these.
Why would you use an API call when there is a built in VB6 function that does exactly the same thing? As I said in my last post the performance gain by using the API call would be negligible as this function would only have once, ie. when the application is started.
Also, why write a whole heap of what could be deemed complicated code to a new programmer, when you can do the same thing in four lines using one simple If ... Then ... statement?
I doubt that your particular answer has answered anyone's questions in this thread.
- Aurilus
<apology>
:eek: JK means Just Kidding. Just trying to have a little fun...No offense intended...
You're exactly right: In this instance, why complicate the code? The VB app.previnstance property is probably more efficient, and definitely easier to read.
You just happened to pick one of my favorite topics: api and pointers. If this were a C-forum, I'd probably be cracking jokes in the pointer threads.
</apology>
Thanks. :D I didn't mean to go off at you, but I'd had a pretty lousy day.
So can anyone explain why you would use Mutex in a situation like this?
- Aurilus
Trying to redeem myself:
As I understand it, you can use mutex with more than just executables. If you want "mutually exclusive" access to a comm port, file, etc., you can use use mutex to see if such access is possible. In the case of checking for a previous instance of an executable, this solution may not be the best one. *BUT* if you were going to check for previous existance of the executable, then check to see if a comm port was in use, then check the status of a file for i/o, using the api call *may* be more desirable for consistency's sake.
Ah, I see my post caused lots of conversation.
Anyway, the reason you would use the API......
is......
its fun!
New users should use the API, its fun. And this IS the API forum. If you learn how to do everything in the API, then you'll have lots less to relearn when you go to another language. So there.
what if in this if block i want to do some action on my prev instance of application means i want to do some action on my already running application , if i try to do any action in this if block it opens the new instance and then close it ...... all action done before End are happen in new instance ... how to do it in prev application ???
If App.PrevInstance = True Then
' all action done here happned on new instance on application
End
End If
actually i want to show main form of my prev application ( if it minimised it should come up means i want to set main form to vbnormal ) when second instance of application is started .. and close the second new instance...
any tips on this ??