问题
I have a simple WPF app which creates a Thread, polls the clipboard every second and trims any strings it finds
However, in the background thread, once the string content changes, the clipboard methods fail with the exception
OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
Example: I have "ABC" on my clipboard and launch the app. A messagebox will popup with the string ABC. Now I copy a string "DEF" and instead of a message box popping up, the application crashes with the above error
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Thread t = new Thread(new ThreadStart(cleanStr));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
void cleanStr()
{
string prevStr = "";
int err = 0;
while (true)
{
if (Clipboard.ContainsText() && !prevStr.Equals(Clipboard.GetText()))
{
prevStr = Clipboard.GetText();
prevStr=prevStr.Trim();
Clipboard.SetText(prevStr);
MessageBox.Show(prevStr);
Thread.Sleep(1000);
}
}
}
来源:https://stackoverflow.com/questions/31213444/openclipboard-fails-with-0x800401d0-clipbrd-e-cant-open