hi,
this is my first post to this group.... its about forcing your app to have only one instance running..
you need to import 2 dlls to find a window and to set focus to that window
/////////
[DllImport("User32",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("User32",EntryPoint="SetForegroundWindow")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[STAThread]
static void Main()
{
Process ThisProcess = Process.GetCurrentProcess();
Process [] AllProcesses = Process.GetProcessesByName(ThisProcess.ProcessName);
if (AllProcesses.Length > 1) // if the process is running
{
bool ret;
IntPtr hWnd = FindWindow(null, "Form1"); //the caption of your window
if(hWnd != IntPtr.Zero)
{
ret = SetForegroundWindow(hWnd); // give focus to this window
{
ret = SetForegroundWindow(hWnd); // give focus to this window
//you can also use BringWindowToTop instead of this
}
}
else
{//run the new process
Application.Run(new Form1());
}
}
}
}
else
{//run the new process
Application.Run(new Form1());
}
}