使用SendKey.SendWait()时出现c#:“System.InvalidOperationException:Queue empty”和其他错误

时间:2011-04-19 08:17:17

标签: c# windows-xp sendkeys invalidoperationexception

我写了一个程序,将'Enter'键发送到某个活动窗口。我使用Timer来获取当前活动窗口的标题并采取相应措施。我制作了一个错误日志文件,以便我可以跟踪所有错误。这是创建错误的代码:

    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        try
        {
            ttl = GetActiveWindowTitle();
            if (ttl != null)
            {
                if (ttl.ToLower() == "xxxxxxxxxxx")
                {
                    SendKeys.SendWait("{ENTER}");
                }
            }
        }
        catch (Exception err)
        {
            Write2ErrLog(err, "OnTimedEvent");
        }
    }

这是GetActiveWindowTitle()方法。

    static private string GetActiveWindowTitle()
    {
        try
        {
            const int nChars = 256;
            IntPtr handle = IntPtr.Zero;
            StringBuilder Buff = new StringBuilder(nChars);
            handle = GetForegroundWindow();

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                return Buff.ToString();
            }
            return null;
        }
        catch (Exception e)
        {
            Write2ErrLog(e, "GetActiveWindowTitle");
            return null;
        }
    }

以下是我收到的错误:

4/19/2011 12:57:16 PM: System.InvalidOperationException: Queue empty.
   at System.Collections.Queue.Dequeue()
   at System.Windows.Forms.SendKeys.SendInput(Byte[] oldKeyboardState, Queue previousEvents)
   at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
   at System.Windows.Forms.SendKeys.SendWait(String keys)
   at DataViews_SendKeys.Form1.OnTimedEvent(Object source, ElapsedEventArgs e)  OnTimedEvent

4/19/2011 1:03:11 PM: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
   at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
   at System.Collections.Queue.Clone()
   at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
   at System.Windows.Forms.SendKeys.SendWait(String keys)
   at DataViews_SendKeys.Form1.OnTimedEvent(Object source, ElapsedEventArgs e)  OnTimedEvent

4/19/2011 1:04:00 PM: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.SendKeys.Flush()
   at System.Windows.Forms.SendKeys.Send(String keys, Control control, Boolean wait)
   at System.Windows.Forms.SendKeys.SendWait(String keys)
   at DataViews_SendKeys.Form1.OnTimedEvent(Object source, ElapsedEventArgs e)  OnTimedEvent

我应该提一下,我运行此程序的计算机是配备Intel Xeon处理器的HP服务器计算机,操作系统是Windows XP。该程序在我的笔记本电脑上运行没有错误,但在服务器计算机上没有。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这可能是由于您的服务器上启用了UAC而桌面上没有启用。使用SendKeys.Send()

时,我遇到了相同的行为

要检查的另一件事是您正在构建的.NET版本。请参阅SendKeys Send is Blocked