Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Multiple Instances

Multiple Instances

Scheduled Pinned Locked Moved C#
question
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TyronX
    wrote on last edited by
    #1

    I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?

    M A T 3 Replies Last reply
    0
    • T TyronX

      I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?

      M Offline
      M Offline
      Matt Gerrans
      wrote on last edited by
      #2

      Just answered this exact question a few pages down: RE: Singleton with a twist[^] Matt Gerrans

      T 1 Reply Last reply
      0
      • M Matt Gerrans

        Just answered this exact question a few pages down: RE: Singleton with a twist[^] Matt Gerrans

        T Offline
        T Offline
        TyronX
        wrote on last edited by
        #3

        What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Thx for the answer

        T M 2 Replies Last reply
        0
        • T TyronX

          What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Thx for the answer

          T Offline
          T Offline
          TyronX
          wrote on last edited by
          #4

          Weird. My Program doesn't get a message when the window is hidden.

          M 1 Reply Last reply
          0
          • T TyronX

            I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?

            A Offline
            A Offline
            Allan Eagle
            wrote on last edited by
            #5

            You mentioned that your previous attempt didn't work. Were you using a mutex? string uniqueid = Application.ExecutablePath.Replace(@"\", @"_"); Mutex m = new Mutex(false, uniqueid); if (m.WaitOne(1, true)) { // Application is not running so carrying on running } else { // Application is already running so end here }

            T 1 Reply Last reply
            0
            • A Allan Eagle

              You mentioned that your previous attempt didn't work. Were you using a mutex? string uniqueid = Application.ExecutablePath.Replace(@"\", @"_"); Mutex m = new Mutex(false, uniqueid); if (m.WaitOne(1, true)) { // Application is not running so carrying on running } else { // Application is already running so end here }

              T Offline
              T Offline
              TyronX
              wrote on last edited by
              #6

              >You mentioned that your previous attempt didn't work. Were you using a mutex? No, I didn't say that. But indeed I aint using one cause I don't now I which Namespace the Mutex Class is (and yet I was to lazy to serch for that).

              1 Reply Last reply
              0
              • T TyronX

                What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Thx for the answer

                M Offline
                M Offline
                Matt Gerrans
                wrote on last edited by
                #7

                > What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Well, don't worry, there's a full money-back guarantee. Just send one proof-of-purchase and a self addressed, stamped envelope and you'll get refunded the full amount. If your startup time increased, that's probably because of something else you did. It doesn't take that long to create a mutex. My app starts up in less than a second, so that couldn't be it. Post your profiler logs and I'll believe you. Anyway, next time, I'll be sure and submit everything to a full QA cycle, running it through an exhaustive matrix of OS (including all service packs and languages, of course), hardware and environment configurations before posting any suggestions. Did you miss the "you've got the gist of it and can proceed with the fine-tuning as you like" part? If you expect people to fully vet any suggestions, then you might perhaps state how much you are willing to pay for that work. Matt Gerrans

                T 1 Reply Last reply
                0
                • T TyronX

                  Weird. My Program doesn't get a message when the window is hidden.

                  M Offline
                  M Offline
                  Matt Gerrans
                  wrote on last edited by
                  #8

                  Seems to be a bug in System.Diagnostics.Process that the MainWindowHandle is 0 when the window is hidden. I wonder if the assumption they are making is that if the main window is hidden, it should be treated like a "daemon" or "service" kind of app (and thus it pretends that there is no main window). Anyway, I guess if you are hiding your window, then you could use FindWindow() or EnumWindows() to get the handle instead of System.Diagnostics.Process.GetProcesses(). There are other ways I can think of too... Matt Gerrans

                  1 Reply Last reply
                  0
                  • M Matt Gerrans

                    > What the hell... now it takes 1-2 seconds longer to start up. And btw. your code crashes. It seems to be that some proccess have a read protected process.MainModule Class. If I wrap around a try-catch it works. Well, don't worry, there's a full money-back guarantee. Just send one proof-of-purchase and a self addressed, stamped envelope and you'll get refunded the full amount. If your startup time increased, that's probably because of something else you did. It doesn't take that long to create a mutex. My app starts up in less than a second, so that couldn't be it. Post your profiler logs and I'll believe you. Anyway, next time, I'll be sure and submit everything to a full QA cycle, running it through an exhaustive matrix of OS (including all service packs and languages, of course), hardware and environment configurations before posting any suggestions. Did you miss the "you've got the gist of it and can proceed with the fine-tuning as you like" part? If you expect people to fully vet any suggestions, then you might perhaps state how much you are willing to pay for that work. Matt Gerrans

                    T Offline
                    T Offline
                    TyronX
                    wrote on last edited by
                    #9

                    I'm sorry if my comments sound to offensive/overacting. I alway speak a little bit to rough, I'll try better next time :)

                    M 1 Reply Last reply
                    0
                    • T TyronX

                      I want to avoid, that the user can start multiple instances of my programm. I found an article on CodeProject which helped, but it's a little bit confusing now when the user tries to start the programm and in doesn't start. Therefore I want, that the second instance activates (setting the windowstate to normal, calling Show() and Activate() the first instance (when it's minized or minimized to tray) before it closes itself. How do I do that?

                      T Offline
                      T Offline
                      TyronX
                      wrote on last edited by
                      #10

                      After using Mutexes and also implementing FindFindow if hwnd is zero everything works fine, it's even way faster. Thanks a lot!

                      1 Reply Last reply
                      0
                      • T TyronX

                        I'm sorry if my comments sound to offensive/overacting. I alway speak a little bit to rough, I'll try better next time :)

                        M Offline
                        M Offline
                        Matt Gerrans
                        wrote on last edited by
                        #11

                        Glad to hear it. And glad to hear (in your other post) that you got it all working smoothly. Matt Gerrans

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups