Ejemplo n.º 1
0
        internal static bool CheckExists(string unicalName)
        {
            // http://snipplr.com/view/19272/ - C#, single-instance-check using mutex
            // http://iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx
            bool isOwnedHere;

            s_AppStartMutex = new Mutex(true, unicalName, out isOwnedHere);
            if (isOwnedHere)
            {
                return(false);
            }

            Process me = Process.GetCurrentProcess();

            Process[] procList = Process.GetProcessesByName(me.ProcessName);
            foreach (Process process in procList) // Debug note: Set Enable the Visual Studio Hosting Process = false.
            {
                if (process.Id != me.Id)          // If the ProcessName matches but the Id doesn't, it's another instance of mine.
                {
                    if (process.MainWindowHandle != IntPtr.Zero)
                    {
                        var wndRef = new HandleRef(process, process.MainWindowHandle);
                        UnsafeNativeMethods.ShowWindowAsync(wndRef, NativeMethods.SW_NORMAL);
                        UnsafeNativeMethods.SetForegroundWindow(wndRef);
                    }
                    else
                    {
                        var callback = new SafeNativeMethods.EnumThreadWindowsCallback(FindInvisibleWindow);
                        SafeNativeMethods.EnumWindows(callback, new IntPtr(process.Id));
                        GC.KeepAlive(callback);
                    }
                    break;
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
Archivo: Form.cs Proyecto: mind0n/hive
        internal override void RecreateHandleCore() {
            //Debug.Assert( CanRecreateHandle(), "Recreating handle when form is not ready yet." );
            NativeMethods.WINDOWPLACEMENT wp = new NativeMethods.WINDOWPLACEMENT();
            FormStartPosition oldStartPosition = FormStartPosition.Manual;

            if (!IsMdiChild && (WindowState == FormWindowState.Minimized || WindowState == FormWindowState.Maximized)) {
                wp.length = Marshal.SizeOf(typeof(NativeMethods.WINDOWPLACEMENT));
                UnsafeNativeMethods.GetWindowPlacement(new HandleRef(this, Handle), ref wp);
            }

            if (StartPosition != FormStartPosition.Manual) {
                oldStartPosition = StartPosition;
                // Set the startup postion to manual, to stop the form from
                // changing position each time RecreateHandle() is called.
                StartPosition = FormStartPosition.Manual;
            }

            EnumThreadWindowsCallback etwcb = null;
            SafeNativeMethods.EnumThreadWindowsCallback callback = null;
            if (IsHandleCreated) {
                // First put all the owned windows into a list
                etwcb = new EnumThreadWindowsCallback();
                if (etwcb != null) {
                    callback = new SafeNativeMethods.EnumThreadWindowsCallback(etwcb.Callback);
                    UnsafeNativeMethods.EnumThreadWindows(SafeNativeMethods.GetCurrentThreadId(),
                                                          new NativeMethods.EnumThreadWindowsCallback(callback),
                                                          new HandleRef(this, this.Handle));
                    // Reset the owner of the windows in the list
                    etwcb.ResetOwners();
                }
            }

            base.RecreateHandleCore();


            if (etwcb != null) {
                // Set the owner of the windows in the list back to the new Form's handle
                etwcb.SetOwners(new HandleRef(this, this.Handle));
            }
            
            if (oldStartPosition != FormStartPosition.Manual) {
                StartPosition = oldStartPosition;
            }

            if (wp.length > 0) {
                UnsafeNativeMethods.SetWindowPlacement(new HandleRef(this, Handle), ref wp);
            }

            if (callback != null) {
                GC.KeepAlive(callback);
            }
        }