Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 118939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:34:26+00:00 2026-05-11T03:34:26+00:00

I have an application which may only have one instance of itself open at

  • 0

I have an application which may only have one instance of itself open at a time. To enforce this, I use this code:

System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses(); System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess(); foreach (System.Diagnostics.Process p in myProcesses) {     if (p.ProcessName == me.ProcessName)         if (p.Id != me.Id)         {             //if already running, abort this copy.             return;         } } //launch the application. //... 

It works fine. I would also like it to be able to focus the form of the already-running copy. That is, before returning, I want to bring the other instance of this application into the foreground.

How do I do that?

SetForegroundWindow works, to a point:

[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd);   // ... if (p.Id != me.Id) {     //if already running, focus it, and then abort this copy.     SetForegroundWindow(p.MainWindowHandle);     return; } // ... 

This does bring the window to the foreground if it is not minimized. Awesome. If the window IS minimized, however, it remains minimized.

It needs to un-minimize.

Solution via SwitchToThisWindow (Works!):

[System.Runtime.InteropServices.DllImport("user32.dll")] public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);  [STAThread] static void Main() {     System.Diagnostics.Process me = System.Diagnostics.Process.GetCurrentProcess();     System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcessesByName(me.ProcessName);     foreach (System.Diagnostics.Process p in myProcesses)     {         if (p.Id != me.Id)         {             SwitchToThisWindow(p.MainWindowHandle, true);             return;         }     }     //now go ahead and start our application ;-) } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:34:27+00:00Added an answer on May 11, 2026 at 3:34 am

    I had the same problem and SwitchToThisWindow() worked the best for me. The only limitation is that you must have XP sp1 installed. I played with SetForegroundWindow, ShowWindow, and they both had problems pulling the window into view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application which needs to use a dll (also written by me) which
I have an application which resides in ROOT. This application has a java class(in
I'm working on a flex 3 application which will initially support only one language
I have an application, which resolves DNS hostnames to IP in one of its
I have an application which models the electrical distribution of a system whereby if
The application I'm working on is using a scanner, which may belong to one
Most web applications have a Location field, in which uses may enter a Location
I have an application which loads up c# source files dynamically and runs them
I have an application which needs to loop through all the lines in text
I have an application which has a similar interface to Visual Studio, in that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.