Single instance of application
-
Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application
-
Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application
Hy, Use the
Process.GetProcessesByName(processname)
class to see if another process with the same name is running. Hope it helps.Do your best to be the best
-
Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application
There are some articles here on CP covering this topic. Simply search the artciles for "single instance application".
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hy, Use the
Process.GetProcessesByName(processname)
class to see if another process with the same name is running. Hope it helps.Do your best to be the best
-
Hi I devloped a application.Every time clicking on .exe a instance is opened of that application.How this is possible that single instance run of this application
This is the code your looking for.
[STAThread]
static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.Application.Run(new AteGui(splashScrn)); } else { //There is another instance of this process. HandleRunningInstance(instance); } }
Helper Functions and DLL imports
/// /// Property to set the visible status of the Tutorial Button
///
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);//Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\\\") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } public static void HandleRunningInstance(Process instance) { //Make sure the window is not minimized or maximized ShowWindowAsync(instance.MainWindowHandle, WS\_SHOWNORMAL); //Set the real intance to foreground window SetForegroundWindow(instance.MainWindowHandle); } \[DllImport("User32.dll")\] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); \[DllImport("User32.dll")\] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS\_SHOWNORMAL = 1;
-
This is the code your looking for.
[STAThread]
static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.Application.Run(new AteGui(splashScrn)); } else { //There is another instance of this process. HandleRunningInstance(instance); } }
Helper Functions and DLL imports
/// /// Property to set the visible status of the Tutorial Button
///
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);//Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\\\") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } public static void HandleRunningInstance(Process instance) { //Make sure the window is not minimized or maximized ShowWindowAsync(instance.MainWindowHandle, WS\_SHOWNORMAL); //Set the real intance to foreground window SetForegroundWindow(instance.MainWindowHandle); } \[DllImport("User32.dll")\] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); \[DllImport("User32.dll")\] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS\_SHOWNORMAL = 1;
As has been stated before, iterating through a process list is not a good idea because of several reasons (with being slow the least one). Mutexes are the way to go!
Regards, mav -- Black holes are the places where God divided by 0...