22

This has been a problem that I haven't been able to figure out for sometime. Preventing the second instance is trivial and has many methods, however, bringing back the already running process isn't. I would like to:

  • Minimized: Undo the minimize and bring the running instance to the front.
  • Behind other windows: Bring the application to the front.

The language I am using this in is VB.NET and C#.

MagicKat
  • 9,695
  • 6
  • 32
  • 43

6 Answers6

13

I found this code to be useful. It does the detection and optional activation of an existing application:

http://www.codeproject.com/KB/cs/cssingprocess.aspx

Big GH
  • 1,321
  • 2
  • 17
  • 29
8

If you're using .NET, this seems easier and more straightforward using build-in .NET functionality:

The Weekly Source Code 31- Single Instance WinForms and Microsoft.VisualBasic.dll

Tim Erickson
  • 2,323
  • 2
  • 22
  • 27
  • +1 - A much cleaner approach that uses the Microsoft.VisualBasic assembly instead of all the Win32 API gunk in the accepted answer. Also allows you to pass command line arguments back to the running instance - a nice bonus. – Tom Bushell Dec 07 '09 at 22:13
2

These link may be of help:

http://www.ai.uga.edu/mc/SingleInstance.html

It has code to detect another instance running, not sure what you can do with it once you've got the instance though.

Mark Ingram
  • 71,849
  • 51
  • 176
  • 230
1

In Form_Load this code worked.

 If App.PrevInstance = True Then
     MsgBox "Already running...."
     Unload Me
     Exit Sub
 End If
0

Here is a simple and easily understandable method for preventing duplicate concurrent execution (written in c#).

public static void StopProgramOnSecondRun()
{
    string
        //Get the full filename and path
        FullEXEPath = System.Reflection.Assembly.GetEntryAssembly().Location,
        //Isolate just the filename with no extension
        FilenameWithNoExtension = System.IO.Path.GetFileNameWithoutExtension(FullEXEPath);

    //Retrieve a list of processes that have the same name as this one wich is FilenameWithNoExtension
    Process[] processes = System.Diagnostics.Process.GetProcessesByName(FilenameWithNoExtension);

    //There should always be at least one process returned.  If the number is greater than one.  Than this is the clone and we must kill it.
    if (processes.Length > 1)
        System.Diagnostics.Process.GetCurrentProcess().Kill();
}
Jason Geiger
  • 1,912
  • 18
  • 32
0

I used the FileSystemWatcher on the form to solve this. This solution checks for the process, does not start a new instance, and shows the form of the already running process.

Add a FileSystemWatcher to the form that checks for the creation of a file and then shows the form with the created event.

In Program.cs:

if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                File.Create("AlreadyRunning.log").Dispose();
                return;
            }

For the form's FileSystemWatcher created event:

if (File.Exists("AlreadyRunning.log"))
            {
                Show();
                WindowState = FormWindowState.Normal;
                File.Delete("AlreadyRunning.log");
            }
sixin
  • 21
  • 1
  • 6