CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Join Date
    Dec 2018
    Posts
    11

    Question App.Previnstance

    I have an application that uses app.previnstance to check if a copy of the program is already running. This almost always works well (and this program is used by hundreds of people every day and has been for over 15 years) but every now and then it fails and refuses to start saying it's already running even though it isn't. Whether this is because app.previnstance is wrong or it's because somehow Windows wrongly thinks it's still running I don't know.

    Question 1: How exactly does app.previnstance work? What/how is it internally checking?

    Question 2: Is there a "better" way of doing this? One which is guaranteed never to fail?

    Thanks
    Ian

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

  3. #3
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Thanks for that VictorN, it's an interesting reply with some interesting links. However I'm uncertain if it actually addresses my issues (perhaps because I don't fully understand Mutex).
    Looking at app.previnstance, as I said, the majority of the time it works fine, it's only rare occasions that it seems to fail, saying the program is running when it shouldn't be, it's like it tried to finish but some reference to it wasn't removed but without knowing what app.previnstance does I can't tell what the problem is.
    That being the case, isn't it possible that the same issue could happen with a Mutex? If some part of the program is not being cleared (the bit causing app.previnstance to fail) could that uncleared portion not be holding the Mutex and so that method could suffer from the same problem?
    Does anyone know what app.previnstance checks, or does anyone know why this problem happens?
    Can it happen with a Mutex? Why not?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: App.Previnstance

    Maybe the Wikipedia would be easier for you to understand the Mutual exclusion
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: App.Previnstance

    Have a look at this post. It has an example using mutex.

    http://forums.codeguru.com/showthrea...vious-instance
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Thanks but I've tried a Mutex and it suffers from the same problem, if the program takes the Mutex then crashes, or even exits without releasing it then the mutex is locked and the only thing that seems to work is to reboot the computer.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: App.Previnstance

    Another way is to obtain the current .exe name (process name), enumerate all running processes and determine if the current process name is already running.

    To get the current .exe name see http://www.vbforums.com/showthread.p...B6-Application

    To enumerate all running processes, see http://www.vbforums.com/showthread.p...ning-processes
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: App.Previnstance

    Quote Originally Posted by ianbrooke View Post
    Thanks but I've tried a Mutex and it suffers from the same problem, if the program takes the Mutex then crashes, or even exits without releasing it then the mutex is locked and the only thing that seems to work is to reboot the computer.
    how do you prove the previous instance/mutex?
    Did you read the Joe.Newcomer's essay I referred to?
    Victor Nijegorodov

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: App.Previnstance

    Fix the bug in your program causing it to not shutdown cleanly. Then use a named mutex.

  10. #10
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Quote Originally Posted by Arjay View Post
    Fix the bug in your program causing it to not shutdown cleanly. Then use a named mutex.
    Seeing as you're such a know-all, perhaps you could tell me where this bug is and what it looks like. Like I said this program is over 15 years old, it has been used hundreds of thousands of times and has failed like this single-figure times. I have no idea where a bug is if there is one, there's over 30,000 lines of code spread over multiple forms, modules and classes and I've spent many many hours looking yet can find nothing wrong. A more useful suggestion would be welcome.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: App.Previnstance

    Quote Originally Posted by ianbrooke View Post
    Seeing as you're such a know-all, perhaps you could tell me where this bug is and what it looks like. Like I said this program is over 15 years old, it has been used hundreds of thousands of times and has failed like this single-figure times. I have no idea where a bug is if there is one, there's over 30,000 lines of code spread over multiple forms, modules and classes and I've spent many many hours looking yet can find nothing wrong. A more useful suggestion would be welcome.
    Not sure about being a know-all, but I do have some experience in this area. Such as an article I've written 15 years ago that contains a sample called 'OnlyOne'. It uses a named mutex to limit an application to a single instance, if a second is opened it brings the first instance to the foreground. It does this by using a memory backed memory mapped file to share the hwnd of the main window of the first instance with the 2nd instance. The 2nd instance brings the hwnd to the foreground before exiting. The article is here (with VC++ code samples): https://www.codeguru.com/cpp/misc/mi...er-Classes.htm

    At any rate, googling for 'how does app.preinstance work in vb6' tells me that it only works when the 2nd copy of the app is started from the same location as the first copy. It is possible that the occasional time it doesn't work is because the user has opened the app from a different location?

    As far as what to do next, it would probably too much to suggest you add logging/tracing to your program to find out what is going on during shutdown (such as any ActiveX controls that you may call, hanging up). If you haven't found the issue in your code in 15 years, it is unlikely that tracing is going to help, though.

    Another approach is to use the mutex/shared memory approach to limit the app to a single instance and forget about using app.preinstance (given its limitations). Probably the simplest way to do this is put the mutex/memory mapped file code into a dll using C++, expose a couple of c-style functions and then call it from vb6.

  12. #12
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Thank you for your suggestions. I too have been developing software for over 30 years and know what to look for in fixing problems, this one has me beat and it's very frustrating. There is no chance that it's being run from different locations. No-one yet has been able to tell me exactly what app.previnstance does and I tend to think that knowing that would at least point me in the direction of the problem. I still question the mutex idea solely because it could suffer the same problem (and I did try it out in a test program the other day). If something is causing the program to exit without clearing whatever app.previnstance checks then it is likely to do the same with a mutex, and indeed if I stop my test program after it has taken a named mutex and try to run it again it fails to start as it can't take the mutex. For all I know app.previnstance may just take a mutex.
    However, If the program takes a mutex and then remembers the integer returned for example in a registry key, the next run of the program can pick up that key and destroy the mutex and proceed. This led me to thinking that if the program can proceed then what's the point in stopping on app.previnstance, I can change a few things so there are no files held open such that multiple instances don't matter. I can then simply use app.previnstance as a check, warn the user there MAY be another instance running and ask if they want to continue, if they do then simply ignore the other instance. It's going to take some thinking about but it may work.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: App.Previnstance

    If part/all of the title of the main window is known and is unique to your application, then you can check if a window containing that title is already present or not. For sample code see http://vb.mvps.org/samples/FindPart/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: App.Previnstance

    Quote Originally Posted by ianbrooke View Post
    Thanks but I've tried a Mutex and it suffers from the same problem, if the program takes the Mutex then crashes, or even exits without releasing it then the mutex is locked and the only thing that seems to work is to reboot the computer.
    There is a discussion about such a problem here:
    https://stackoverflow.com/questions/...rocess-crashes
    And there is addition info:
    https://docs.microsoft.com/en-us/win...orsingleobject
    Victor Nijegorodov

  15. #15
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    That's really interesting. Thank you!

Page 1 of 2 12 LastLast

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

Featured