Search results for 'Force Single Instance'. 1 post(s) found.
In the program's Main method, call ProcessUtils.ThisProcessIsAlreadyRunning() to determine if another instance of the program is already running. ThisProcessIsAlreadyRunning attempts to create a Mutex object with the same name as the program (Application.ProductName). If it fails, the Mutex object already exists (and was created by another instance of the same program).
If another instance of the program is already running, that instance is made visible and input focus is set to it. Otherwise a new instance of the program is launched.
Program.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PU;
namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
// If this program is already running, set focus
// to that instance and quit.
if (ProcessUtils.ThisProcessIsAlreadyRunning())
{
// "Form1" is the caption (Text property) of the main form.
ProcessUtils.SetFocusToPreviousInstance("Form1");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using PU;
namespace WindowsApplication1
{
static class Program
{
[STAThread]
static void Main()
{
// If this program is already running, set focus
// to that instance and quit.
if (ProcessUtils.ThisProcessIsAlreadyRunning())
{
// "Form1" is the caption (Text property) of the main form.
ProcessUtils.SetFocusToPreviousInstance("Form1");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}
ProcessUtils.cs:
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PU
{
/// Summary description for ProcessUtils.
public static class ProcessUtils
{
private static Mutex Mutex = null;
/// Determine if the current process is already running
public static bool ThisProcessIsAlreadyRunning()
{
// Only want to call this method once, at startup.
Debug.Assert(Mutex == null);
// createdNew needs to be false in .Net 2.0, otherwise, if another instance of
// this program is running, the Mutex constructor will block, and then throw
// an exception if the other instance is shut down.
bool createdNew = false;
Mutex = new Mutex(false, Application.ProductName, out createdNew);
Debug.Assert(Mutex != null);
return !createdNew;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_RESTORE = 9;
[DllImport("user32.dll")]
static extern IntPtr GetLastActivePopup(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsWindowEnabled(IntPtr hWnd);
/// Set focus to the previous instance of the specified program.
public static void SetFocusToPreviousInstance(string windowCaption)
{
// Look for previous instance of this program.
IntPtr hWnd = FindWindow(null, windowCaption);
// If a previous instance of this program was found...
if (hWnd != null)
{
// Is it displaying a popup window?
IntPtr hPopupWnd = GetLastActivePopup(hWnd);
// If so, set focus to the popup window. Otherwise set focus
// to the program's main window.
if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
{
hWnd = hPopupWnd;
}
SetForegroundWindow(hWnd);
// If program is minimized, restore it.
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
}
}
}
}
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace PU
{
/// Summary description for ProcessUtils.
public static class ProcessUtils
{
private static Mutex Mutex = null;
/// Determine if the current process is already running
public static bool ThisProcessIsAlreadyRunning()
{
// Only want to call this method once, at startup.
Debug.Assert(Mutex == null);
// createdNew needs to be false in .Net 2.0, otherwise, if another instance of
// this program is running, the Mutex constructor will block, and then throw
// an exception if the other instance is shut down.
bool createdNew = false;
Mutex = new Mutex(false, Application.ProductName, out createdNew);
Debug.Assert(Mutex != null);
return !createdNew;
}
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_RESTORE = 9;
[DllImport("user32.dll")]
static extern IntPtr GetLastActivePopup(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool IsWindowEnabled(IntPtr hWnd);
/// Set focus to the previous instance of the specified program.
public static void SetFocusToPreviousInstance(string windowCaption)
{
// Look for previous instance of this program.
IntPtr hWnd = FindWindow(null, windowCaption);
// If a previous instance of this program was found...
if (hWnd != null)
{
// Is it displaying a popup window?
IntPtr hPopupWnd = GetLastActivePopup(hWnd);
// If so, set focus to the popup window. Otherwise set focus
// to the program's main window.
if (hPopupWnd != null && IsWindowEnabled(hPopupWnd))
{
hWnd = hPopupWnd;
}
SetForegroundWindow(hWnd);
// If program is minimized, restore it.
if (IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}
}
}
}
}
Another posts included in "C#"
| How to exit from a console application (0) | 2007/09/25 |
| The best way to enable and disable buttons, menu items and toolbar buttons (0) | 2007/09/25 |
| How to detect resource leaks in a Form (0) | 2007/09/25 |
| Compress and decompress strings in C# (0) | 2007/09/25 |
| Graphics in C# - Irregular Forms (0) | 2007/09/25 |
| Extracting the Country from IP Address (0) | 2007/09/25 |
| String Replace Function For C# (0) | 2007/08/29 |
| How to print character in C# by ASCII code (0) | 2007/08/29 |
Trackback : Cannot send a trackbact to this post.
-
Subject different money making ideas
2010/01/29 05:59
moneyideas
-
Subject different money making ideas
2010/01/29 14:32
moneyideas
-
Subject different money making ideas
2010/01/31 16:41
moneyideas

Prev

Rss Feed