Having exception with Clipboard while using Inputsimulator

1

My project is about text processing software and using InputSimulator library.

When my program simulate Ctrl+C and get text from clipboard, It gave an exception like this.

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in     PresentationCore.dll

Additional information: OpenClipboard Failed (Exception from HRESULT:0x800401D0 (CLIPBRD_E_CANT_OPEN))

here's my project and here's my code

        System.Threading.Thread.Sleep(3000); // wait for opening notepad and type any text to test
        while (true)
        {
            string oldclipboard = Clipboard.GetText();
            InputSimulator input = new InputSimulator();
            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_A); // select text

            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C);
            string highlightedtext = Clipboard.GetText();
            //do sth.(text processing)
            input.Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_V);
        }

I'm sure that it is about conflict between the library and clipboard. but I don't know what is the problem.

So, Why it is throwing an exception? and Is there any alternative way to to this?

Thanks

P.S. I've also try "Sendkeys.Sendwait". It didn't give any exception but it is unstable.

c#
wpf
asked on Stack Overflow Dec 4, 2015 by doliolarzz • edited Dec 4, 2015 by doliolarzz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0