/// <summary> /// Copy the current code to the clipboard /// </summary> public void CopyCodeToClipboard(string code = null, bool showError = false) { if (code == null) { code = this.CurrentCode; } bool clipRetry = false; do { bool failed = false; // check if the clipboard is locked IntPtr hWnd = User32Window.GetOpenClipboardWindow(); if (hWnd != IntPtr.Zero) { int len = User32Window.GetWindowTextLength(hWnd); if (len == 0) { //WinAuthMain.LogException(new ApplicationException("Clipboard in use by another process")); } else { StringBuilder sb = new StringBuilder(len + 1); User32Window.GetWindowText(hWnd, sb, sb.Capacity); //WinAuthMain.LogException(new ApplicationException("Clipboard in use by '" + sb.ToString() + "'")); } failed = true; } else { // Issue#170: can still get error copying even though it works, so just increase retries and ignore error try { Clipboard.Clear(); // add delay for clip error System.Threading.Thread.Sleep(100); Clipboard.SetDataObject(code, true); } catch (ExternalException) { } } if (failed == true && showError == true) { // only show an error the first time //clipRetry = (MessageBox.Show(form, strings.ClipboardInUse, // WinAuthMain.APPLICATION_NAME, // MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes); } }while (clipRetry == true); }