C# 使用SendKeys()时-InvalidOperationException:遇到撤消操作

C# 使用SendKeys()时-InvalidOperationException:遇到撤消操作,c#,sendkeys,invalidoperationexception,C#,Sendkeys,Invalidoperationexception,这是我的密码 public void KeyPress() { //Finds the target window and sends a key command to the application Process[] processes = Process.GetProcessesByName("calc"); IntPtr calculatorHandle; foreach (Process proc in proce

这是我的密码

public void KeyPress()
    {
        //Finds the target window and sends a key command to the application
        Process[] processes = Process.GetProcessesByName("calc");
        IntPtr calculatorHandle;
        foreach (Process proc in processes)
        {
            calculatorHandle = proc.MainWindowHandle;

            if (calculatorHandle == IntPtr.Zero)
            {
                MessageBox.Show("Calculator is not running.");
                return;
            }
            SetForegroundWindow(calculatorHandle);

            break;
        }

        SendKeys.SendWait("1");

    }
执行此代码后,我收到一个错误,我知道源是SendKeys

这是我收到的全部错误

System.InvalidOperationException was unhandled
  Message="The Undo operation encountered a context that is different from what was applied in the corresponding Set operation. The possible cause is that a context was Set on the thread and not reverted(undone)."
  Source="mscorlib"
  StackTrace:
       at System.Threading.SynchronizationContextSwitcher.Undo()
       at System.Threading.ExecutionContextSwitcher.Undo()
       at System.Threading.ExecutionContext.runFinallyCode(Object userData, Boolean exceptionThrown)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteBackoutCodeHelper(Object backoutCode, Object userData, Boolean exceptionThrown)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.ContextAwareResult.Complete(IntPtr userToken)
       at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
       at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  InnerException: 

我不确定问题出在哪里,这个数字会出现在我的计算器中,但会弹出错误

我自己玩过这个,无法复制它

做了一个谷歌搜索,找到了下面的文章,其中有不少建议,你可能想试试。

当然,你可能已经看过了,这就是你来这里的原因

您可能需要提供有关调用代码的上下文的更广泛信息。该应用程序的功能是什么,如何适应?你能告诉我它在哪一行代码上失败了吗


另外,正如@SLaks所建议的,您能提供关于内部异常的更多详细信息吗?

好的,根据您对我的问题的回答,我将做出两个假设

  • 您正在从异步执行的函数调用KeyPress,可能是从BeginReceive或类似的回调函数调用

  • 您的应用程序是windows窗体应用程序


  • 如果上述假设是正确的,那么我怀疑问题在于SendKeys在内部使用windows消息传递,而问题的发生是因为SendkKeys消息是从UI线程以外的线程发送的。如果这是正确的,您可以通过调用按键功能来解决问题。使用
    Control.Invoke
    会将调用封送回UI线程。

    此错误与
    SendKeys
    无关。什么是
    InnerException
    ?您是否正在执行异步IO?是否在应用程序中使用套接字?问题似乎与完成异步套接字操作有关。@SLaks-我相信我们使用的是异步I/O。InnerException为NULL@克里斯-是的,我们正在使用套接字,传递一个变量来触发函数。在命令站上按下按钮的应用程序将触发客户端上的功能。并使用sendkeys模拟客户端应用程序中的按钮按下。需要更多信息吗?调用
    KeyPress
    的位置在哪里?我必须使用Control.Invoke方法(委托,Object[])我有另一个程序员在处理这个项目,还有这个控件。Invoke加上一个私有委托,程序可以运行了。谢谢你+1链接到线程。尽管social.msdn.microsoft.com上还有其他相关的线程,但与提供的链接相比,没有任何线程。谢谢