assume that 2 users are logged in, on a single machine (through remote desktop connection), and both the users try to run an instance of the app, will this code wurk ??
Thanks,
Asim
-----Original Message-----
From: fahad [mailto:desperate_desperado@...]
Sent: Monday, July 21, 2003 3:46 PM
To: codesnips@yahoogroups.com
Subject: [codesnips] Single Instance appshi,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 windowif(hWnd != IntPtr.Zero)
{
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());
}
}
To unsubscribe from this group, send an email to:
codesnips-unsubscribe@yahoogroups.com
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.