OpenClipboard fails with 0x800401D0 (CLIPBRD_E_CANT_OPEN))

4

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);
                    }
            }
        }
c#
wpf
windows-8.1
asked on Stack Overflow Jul 3, 2015 by Akash • edited Jul 4, 2015 by Akash

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0