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. Show 1st instance of application when starting 2nd instance?

Show 1st instance of application when starting 2nd instance?

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • K Offline
    K Offline
    Kasdoffe
    wrote on last edited by
    #1

    When my program starts, I want to check if it's already running. If it is running, I want to show it and bring it to the foreground. I know multiple ways of checking if my program is already running by using the Process class or Mutex class. However, my main problem comes when trying to get my program to show itself. I'm using a NotifyIcon and ShowInTaskBar is set to false. When my program is minimized to the system tray and the second process starts and uses the Process class to get the MainWindowHandle of the 1st process, it returns 0. I experimented with it and found that if I kept my program in the taskbar by setting ShowInTaskbar to true, it would have a non-zero MainWindowHandle I can use. But, I don't want it in the taskbar. So, how can reactivate my already running program if I try to start a 2nd instance of it when the MainWindowHandle is 0? I probably could save the window handle to a file (suck!) and use it when a second instance starts. Help please?

    T 1 Reply Last reply
    0
    • K Kasdoffe

      When my program starts, I want to check if it's already running. If it is running, I want to show it and bring it to the foreground. I know multiple ways of checking if my program is already running by using the Process class or Mutex class. However, my main problem comes when trying to get my program to show itself. I'm using a NotifyIcon and ShowInTaskBar is set to false. When my program is minimized to the system tray and the second process starts and uses the Process class to get the MainWindowHandle of the 1st process, it returns 0. I experimented with it and found that if I kept my program in the taskbar by setting ShowInTaskbar to true, it would have a non-zero MainWindowHandle I can use. But, I don't want it in the taskbar. So, how can reactivate my already running program if I try to start a 2nd instance of it when the MainWindowHandle is 0? I probably could save the window handle to a file (suck!) and use it when a second instance starts. Help please?

      T Offline
      T Offline
      TylerBrinks
      wrote on last edited by
      #2

      There are 2 ways I would go about this: The graceful way would be to use remoting. In your code that checks for a single instance you would set up remoting to communicate with the running instance of your program and call a method in that instance to bring itself to the foreground. Remoting is ideal for communicating locally across application and process boundaries. The only drawback is that you need a network card to avoid problems. In the case that you can't guarantee your client's will have a NIC you can use Windows API. You need to call the Activate Window API. Here's a quick intro into that API. http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=711

      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