The problem is that the active window does not change, and pointer GetForegroundWindow changes the value. Ie in my case, when you click on a notebook, the form disappears and appears at the same second.
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); void FindF2B(){ var hWndParent = FindWindow("Notepad", "Untitled - Notepad"); if(GetForegroundWindow() == hWndParent){ if(this.InvokeRequired){ this.Invoke(new MethodInvoker(delegate{ this.Hide();})); } }else{ if(this.InvokeRequired){ this.Invoke(new MethodInvoker(delegate{ this.Show();})); } }