OpenClipboard fails with 0x800401D0 (CLIPBRD_E_CANT_OPEN))

被刻印的时光 ゝ 提交于 2021-01-27 07:40:35

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!