CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: App.Previnstance

  1. #16
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    I've decided to at least give a mutex a try. Does anyone have any sample code? I'm a little confused (and as usual the Microsoft documentation doesn't really help) if the program should first issue a Createmutex and if that fails issue a Waitforsingleobject to determine if it's been abandoned or what the correct sequence is.
    Thanks

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

    Re: App.Previnstance

    Quote Originally Posted by ianbrooke View Post
    I've decided to at least give a mutex a try. Does anyone have any sample code? I'm a little confused (and as usual the Microsoft documentation doesn't really help) if the program should first issue a Createmutex and if that fails issue a Waitforsingleobject to determine if it's been abandoned or what the correct sequence is.
    Thanks
    There is sample code in the article I linked to. It's in C++, but it should give you the general flow. As I mentioned before, in the sample if if finds a previous instance of the program running it sets that program to the foreground before exiting. It does this by sharing the main hWnd of the first instance across processes (so the 2nd instance can access it). For inter-process communication it stores a structure with an hwnd field into a memory mapped file (but there is no 'file' present as the MMF is in-memory). The C++ code sample has wrapper classes that make the MMF work easy. You can't use them in VB, but you can make the same win api calls in vb.

    A word about named mutexes. A named mutex will exist as long as there is at least one process that has a handle to it. When a process finishes using a resource such as a mutex it should call CloseHandle to let Windows know the process is done using it. However, if two processes access a named mutex and neither process calls CloseHandle before exiting, Windows will still destroy the mutex after both processes have exited. In other words, a mutex will not live on after all processes that have accessed the mutex have closed. This is true whether the process has exited cleanly or was terminated by task manager, crashed and closed, or whatever.

    Btw, if your app is functioning as a COM exe server, it is important that any COM clients call release on the COM object so the COM exe server can decrement its ref count. In a COM exe server, the COM sub system will automatically shut it down when the ref count on its interface(s) are 0. Conversely, it will keep it running if any ref counts are > 0. Your app probably isn't acting as a COM exe server, but many apps of that vintage did operate the way (e.g. office apps like Word and Excel are COM exe servers). You can search the registry under the CLSID key to see if your app has any COM registration keys.

    Btw, when you implement the mutex, add some logging to a log file when you find the named mutex already exists. Use the toolhelp 32 functions to snap a list of the system processes. I would expect that you will find two processes with your exe name, the 2nd instance (where you are snapping the process list), and the first instance that hasn't exited.

  3. #18
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Ok, thanks but I never had the opportunity (nor I must say, the desire) to get into C++, I can do C#, F#, F77, Cobol, VB, VB.net and a couple of other more obscure languages but C++ leaves me cold! Any chance anyone of a translation?
    I would add that when this problem happens I'm told that the program is no longer showing on the task bar and as such I can only assume it has closed it's forms, classes etc but somehow failed to end, I would not consider it safe to switch to.
    Last edited by ianbrooke; May 18th, 2020 at 09:57 PM.

  4. #19
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,732

    Re: App.Previnstance

    Maybe something from here could help you:
    Mutex
    Why doesn't Mutex get released when disposed?
    Victor Nijegorodov

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

    Re: App.Previnstance

    Quote Originally Posted by ianbrooke View Post
    I've decided to at least give a mutex a try. Does anyone have any sample code? I'm a little confused (and as usual the Microsoft documentation doesn't really help) if the program should first issue a Createmutex and if that fails issue a Waitforsingleobject to determine if it's been abandoned or what the correct sequence is.
    Thanks
    I gave a link in my post #5 http://forums.codeguru.com/showthrea...vious-instance - see post #11 in that thread.
    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++17 Compiler: Microsoft VS2019 (16.6.3)

  6. #21
    Join Date
    Dec 2018
    Posts
    11

    Re: App.Previnstance

    Quote Originally Posted by 2kaud View Post
    I gave a link in my post #5 http://forums.codeguru.com/showthrea...vious-instance - see post #11 in that thread.
    Yes you did but that code appears to me to be incomplete. That code only uses CreateMutex but as I understand it (and I may be wrong) that won't work in a situation where the Mutex owner has crashed, not exited correctly or whatever. To detect that requires WaitOnSingle Object which that code does not have.

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)