|
收藏的一个 First, to ensure that only one instance of your application is running at a time,博彩, the best method I've found is to create a mutex that is held by the operating system (thanks to ). This will put a request to the operating system that a mutex be created if one does not already exist. Only one mutex can ever be created at a time,六合彩现场开奖, so if you request a new one and it cannot be created, you can safely assume that your application is already running. using System.Threading public class Form1 : Form Mutex m = new Mutex(true, "YourAppName",足球投注网, out createdNew); if (! createdNew) Application.Run( new Form1()); // keep the mutex reference alive until the normal termination of the programGC.KeepAlive(m); } } The above code will work for the vast majority of your needs. It will also run under scenarios where your code is executing with less than FullTrust permissions (see Code Access Security in MSDN for further information). If your application can run with Full Trust permissions, we can take this a step further and find the window of the application instnace already running and bring it to the front for the user: public class Form1 : Form System.Threading.Mutex m = new System.Threading.Mutex(true, "YourAppName", out createdNew); if (! createdNew) if(hWnd != IntPtr.Zero) GetWindowPlacement(hWnd, ref placement); if(placement.showCmd != SW_NORMAL) SetWindowPlacement(hWnd, ref placement); return; Application.Run( new Form1()); // keep the mutex reference alive until the normal termination of the programGC.KeepAlive(m); } private const int SW_NORMAL = 1; //see WinUser.h for definitions private const int SW_RESTORE = 9; [DllImport("User32",EntryPoint="FindWindow")] [DllImport("User32",EntryPoint="SendMessage")] [DllImport( "User32",EntryPoint="SetForegroundWindow")]private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport( "User32",EntryPoint="SetWindowPlacement")]private static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); [DllImport( "User32",博彩开户,EntryPoint="GetWindowPlacement")]private static extern bool GetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl); private struct POINTAPI { public int x; public int y; } private struct RECT { public int left; public int top; public int right; public int bottom; } private struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public POINTAPI ptMinPosition; public POINTAPI ptMaxPosition; public RECT rcNormalPosition; } } As you can see,六合彩即时开奖, with minimal effort, you can easily add a polished touch to your application. This might even help you avoid some extra legwork in ensuring that there are no issues with running multiple instances of your app at the same time that you might have to address. For more information about the Platform Invoke mechanisms to call Win32 API functions, I recommend that you check out by John Mueller and Charles Petzold's seminal classic . Until Longhorn comes out and more of the Windows platform becomes managed, platform invokes and interop will remain a key technology to understand and use to your advantage to fill the gaps left by the Windows Forms framework. UPDATE: |
