From 6f891d4a023bc45ff9e475a441a6640db89166df Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 17 Sep 2022 17:28:57 +0100 Subject: [PATCH] Fixes #2039. Surrounding the Clipboard.Contents with a lock keyword. --- UnitTests/ClipboardTests.cs | 192 ++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 95 deletions(-) diff --git a/UnitTests/ClipboardTests.cs b/UnitTests/ClipboardTests.cs index 25c59d180..4d1a686e5 100644 --- a/UnitTests/ClipboardTests.cs +++ b/UnitTests/ClipboardTests.cs @@ -79,110 +79,112 @@ namespace Terminal.Gui.Core { var exit = false; var getClipText = ""; - Application.Iteration += () => { - if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) { - // using (Process clipExe = new Process { - // StartInfo = new ProcessStartInfo { - // RedirectStandardInput = true, - // FileName = "clip" - // } - // }) { - // clipExe.Start (); - // clipExe.StandardInput.Write (clipText); - // clipExe.StandardInput.Close (); - // var result = clipExe.WaitForExit (500); - // if (result) { - // clipExe.WaitForExit (); - // } - // } + lock (Clipboard.Contents) { + Application.Iteration += () => { + if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) { + // using (Process clipExe = new Process { + // StartInfo = new ProcessStartInfo { + // RedirectStandardInput = true, + // FileName = "clip" + // } + // }) { + // clipExe.Start (); + // clipExe.StandardInput.Write (clipText); + // clipExe.StandardInput.Close (); + // var result = clipExe.WaitForExit (500); + // if (result) { + // clipExe.WaitForExit (); + // } + // } - using (Process pwsh = new Process { - StartInfo = new ProcessStartInfo { - FileName = "powershell", - Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"" - } - }) { - pwsh.Start (); - pwsh.WaitForExit (); - } - getClipText = Clipboard.Contents.ToString (); - - } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) { - using (Process copy = new Process { - StartInfo = new ProcessStartInfo { - RedirectStandardInput = true, - FileName = "pbcopy" - } - }) { - copy.Start (); - copy.StandardInput.Write (clipText); - copy.StandardInput.Close (); - copy.WaitForExit (); - } - getClipText = Clipboard.Contents.ToString (); - - } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) { - if (Is_WSL_Platform ()) { - try { - using (Process bash = new Process { - StartInfo = new ProcessStartInfo { - FileName = "powershell.exe", - Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\"" - } - }) { - bash.Start (); - bash.WaitForExit (); + using (Process pwsh = new Process { + StartInfo = new ProcessStartInfo { + FileName = "powershell", + Arguments = $"-command \"Set-Clipboard -Value \\\"{clipText}\\\"\"" } + }) { + pwsh.Start (); + pwsh.WaitForExit (); + } + getClipText = Clipboard.Contents.ToString (); - //using (Process clipExe = new Process { - // StartInfo = new ProcessStartInfo { - // RedirectStandardInput = true, - // FileName = "clip.exe" - // } - //}) { - // clipExe.Start (); - // clipExe.StandardInput.Write (clipText); - // clipExe.StandardInput.Close (); - // clipExe.WaitForExit (); - // //var result = clipExe.WaitForExit (500); - // //if (result) { - // // clipExe.WaitForExit (); - // //} - //} - } catch { - exit = true; + } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) { + using (Process copy = new Process { + StartInfo = new ProcessStartInfo { + RedirectStandardInput = true, + FileName = "pbcopy" + } + }) { + copy.Start (); + copy.StandardInput.Write (clipText); + copy.StandardInput.Close (); + copy.WaitForExit (); + } + getClipText = Clipboard.Contents.ToString (); + + } else if (RuntimeInformation.IsOSPlatform (OSPlatform.Linux)) { + if (Is_WSL_Platform ()) { + try { + using (Process bash = new Process { + StartInfo = new ProcessStartInfo { + FileName = "powershell.exe", + Arguments = $"-noprofile -command \"Set-Clipboard -Value \\\"{clipText}\\\"\"" + } + }) { + bash.Start (); + bash.WaitForExit (); + } + + //using (Process clipExe = new Process { + // StartInfo = new ProcessStartInfo { + // RedirectStandardInput = true, + // FileName = "clip.exe" + // } + //}) { + // clipExe.Start (); + // clipExe.StandardInput.Write (clipText); + // clipExe.StandardInput.Close (); + // clipExe.WaitForExit (); + // //var result = clipExe.WaitForExit (500); + // //if (result) { + // // clipExe.WaitForExit (); + // //} + //} + } catch { + exit = true; + } + if (!exit) { + getClipText = Clipboard.Contents.ToString (); + } + Application.RequestStop (); + return; + } + if (exit = xclipExists () == false) { + // xclip doesn't exist then exit. + Application.RequestStop (); + return; + } + + using (Process bash = new Process { + StartInfo = new ProcessStartInfo { + FileName = "bash", + Arguments = $"-c \"xclip -sel clip -i\"", + RedirectStandardInput = true, + } + }) { + bash.Start (); + bash.StandardInput.Write (clipText); + bash.StandardInput.Close (); + bash.WaitForExit (); } if (!exit) { getClipText = Clipboard.Contents.ToString (); } - Application.RequestStop (); - return; - } - if (exit = xclipExists () == false) { - // xclip doesn't exist then exit. - Application.RequestStop (); - return; } - using (Process bash = new Process { - StartInfo = new ProcessStartInfo { - FileName = "bash", - Arguments = $"-c \"xclip -sel clip -i\"", - RedirectStandardInput = true, - } - }) { - bash.Start (); - bash.StandardInput.Write (clipText); - bash.StandardInput.Close (); - bash.WaitForExit (); - } - if (!exit) { - getClipText = Clipboard.Contents.ToString (); - } - } - - Application.RequestStop (); - }; + Application.RequestStop (); + }; + } Application.Run ();