When using SendKeys()-InvalidOperationException: Undo Operation encountered
Here is my code
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 == IntPt开发者_如何学JAVAr.Zero)
{
MessageBox.Show("Calculator is not running.");
return;
}
SetForegroundWindow(calculatorHandle);
break;
}
SendKeys.SendWait("1");
}
After Executing this code I recieve an Error, i know the source is the SendKeys.
Here is the full error I am Receiving
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:
I'm not sure what the problem is, The number will appear in my calculator but that error pops up
OK, so based on your response to my question I will make 2 assumptions.
You are calling KeyPress from a function that is being executed asynchronously, possibly from the callback of a BeginReceive or something similar.
Your application is a windows forms application.
If the above assumptions are correct then I suspect that the problem is with the fact that SendKeys using windows messaging internally and the problem is occurring because the SendkKeys messages are being sent from a thread other than the UI thread. If this is correct, you might be able to resolve the problem by using Control.Invoke to make the call to the KeyPress function. Using Control.Invoke
will marshal the call back to the UI thread.
I had a play with this myself and couldn't replicate it.
Did a google search and found the folllowing article which has quite a few suggestions, which you might like to try. http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/48b4a763-7387-46da-8fc2-3e885670f62c
Of course, you may have already looked there, which is why you came here.
You may need to provide more broad information on context in which you are calling the code. What is the prupose of the app, how does this fit in? Can you tell what line of code its failing on?
Also, as @SLaks suggested, can you include more details on the inner exception?
精彩评论