Results 1 to 12 of 12

Thread: How to know if my exe is running?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    2

    Post How to know if my exe is running?

    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

  2. #2
    Aurilus
    Guest
    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..

    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
    That should solve your problem!

    - Aurilus

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    2
    Thanks. Works like a charm. I have never needed to do it so i didnt know.

    Thanks again.

  4. #4
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73

    Mutex

    If you want to do it through the API... you could always create a named Mutex.
    PHP Code:
    Public Const MAX_PATH 260

    Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTESByVal bInitialOwner As LongByVal 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 
    That should work

  5. #5
    Aurilus
    Guest
    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

  6. #6
    Lively Member
    Join Date
    Aug 2001
    Location
    Crossroads of America
    Posts
    72
    > But why do it through the API?

    If you have to ask, then you probably wouldn't understand.

    JK.

  7. #7
    Aurilus
    Guest
    <sarcasm>Thanks JK for your answer. It really helped!</sarcasm>

    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

  8. #8
    Lively Member
    Join Date
    Aug 2001
    Location
    Crossroads of America
    Posts
    72
    <apology>
    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>

  9. #9
    Aurilus
    Guest
    Thanks. 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

  10. #10
    Lively Member
    Join Date
    Aug 2001
    Location
    Crossroads of America
    Posts
    72
    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.

  11. #11
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73
    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.
    if(GetWindowLong(hwnd,GWL_ID)==IDC_MICROSOFT_APPLICATION)
    {
    SetWindowText(hwnd,"I suck.");
    SendMessage(hwnd,WM_START_SUCKING,0,0);
    SendMessage(hwnd,WM_CRASH,0,0);
    }

  12. #12
    Addicted Member chander's Avatar
    Join Date
    Nov 2000
    Location
    New Delhi , India
    Posts
    225
    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 ??
    Chander
    Email:[email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width