How to open the already running application

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aagarwal8@gmail.com

    How to open the already running application

    Hi,

    I have a windows form application, where the requirement is as
    follows...

    If the application is already running, and the user tries to open
    another instance of the application, the already running instance of
    the application should be activated.

    Also my application can be in the system tray (and not be present in
    the taskbar), so even in this condition the application should be
    activated (i.e. application's main screen should be displayed)

    Any help in this regard is appreciated.


    Regards,
    Ankit!
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: How to open the already running application

    On May 30, 9:10 am, aagarw...@gmail .com wrote:
    Hi,
    >
    I have a windows form application, where the requirement is as
    follows...
    >
    If the application is already running, and the user tries to open
    another instance of the application, the already running instance of
    the application should be activated.
    >
    Also my application can be in the system tray (and not be present in
    the taskbar), so even in this condition the application should be
    activated (i.e. application's main screen should be displayed)
    >
    Any help in this regard is appreciated.
    >
    Regards,
    Ankit!
    Hi,

    You have to use something to detect that the app is running already,
    take a look at Mutex
    To activate the prev. instance you have to get the hWnd of it and make
    it the foremost window. Look in the archive of this NG as this is a
    recurrent question

    Comment

    • Peter Duniho

      #3
      Re: How to open the already running application

      On Fri, 30 May 2008 06:10:49 -0700, <aagarwal8@gmai l.comwrote:
      I have a windows form application, where the requirement is as
      follows...
      >
      If the application is already running, and the user tries to open
      another instance of the application, the already running instance of
      the application should be activated.
      >
      Also my application can be in the system tray (and not be present in
      the taskbar), so even in this condition the application should be
      activated (i.e. application's main screen should be displayed)
      As Ignacio says, you can use a named Mutex to detect the case. As for how
      to actually perform the activation, AFAIK there's nothing in .NET that
      allows you to do this. You'll need to use p/invoke with the
      SetForegroundWi ndow() function, and of course that means you'll have to
      actually find the correct window, also via p/invoke (again, AFAIK there's
      nothing in .NET to allow you to enumerate top-level windows from other
      processes).

      There are restrictions on whether your own process will be allowed to
      bring the other window to the foreground. When it can't, the window entry
      in the task bar will flash instead, which is correct behavior. However,
      most likely the new instance of the application will be the foreground
      task and will have the rights to bring the other application to the
      foreground, so I think that normally this will do exactly what you want.

      Pete

      Comment

      • aagarwal8@gmail.com

        #4
        Re: How to open the already running application

        thanks for the inputs Michael...

        However, i was looking forward to an IPC solution. Any leads in that
        regard?

        Ankit!!

        Comment

        • Peter Duniho

          #5
          Re: How to open the already running application

          On Sun, 01 Jun 2008 20:59:53 -0700, <aagarwal8@gmai l.comwrote:
          thanks for the inputs Michael...
          >
          However, i was looking forward to an IPC solution. Any leads in that
          regard?
          In .NET, network i/o (e.g. Socket, UdpClient, TcpClient) is the usual
          solution. I have read comments that said they added named pipes to .NET
          in the latest version, but I haven't looked into that. Named pipes can be
          slightly more convenient, but either approach should work fine.

          Keep in mind that using IPC, you're probably anticipating the application
          in question trying to restore itself. As I mentioned before, there are
          strict rules about when an application can actually be forced to the
          foreground. Those rules generally preclude an application forcing itself
          to the foreground. If that's important to you, you are actually likely to
          be better off having the current foreground application (the newly-started
          instance of the same application) handle bringing the other to the
          foreground.

          Pete

          Comment

          • mpetrotta@gmail.com

            #6
            Re: How to open the already running application

            On Jun 1, 8:59 pm, aagarw...@gmail .com wrote:
            thanks for the inputs Michael...
            >
            However, i was looking forward to an IPC solution. Any leads in that
            regard?
            Look at the MSDN remoting documentation to get started:


            Peter mentioned the possibility of using network channels rather than
            named pipes. This page addresses that choice:


            Michael


            Comment

            Working...