【问题标题】:c#: "System.InvalidOperationException: Queue empty" and other errors while using SendKey.SendWait()c#: "System.InvalidOperationException: Queue empty" 使用 SendKey.SendWait() 时出现其他错误
【发布时间】:2011-08-08 11:34:03
【问题描述】:

我编写了一个程序来将“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;
        }
    }

以下是我收到的错误:

2011 年 4 月 19 日下午 12:57:16:System.InvalidOperationException:队列为空。 在 System.Collections.Queue.Dequeue() 在 System.Windows.Forms.SendKeys.SendInput(字节 [] oldKeyboardState,队列 previousEvents) 在 System.Windows.Forms.SendKeys.Send(字符串键,控制控件,布尔等待) 在 System.Windows.Forms.SendKeys.SendWait(字符串键) 在 DataViews_SendKeys.Form1.OnTimedEvent(对象源,ElapsedEventArgs e)OnTimedEvent 2011 年 4 月 19 日下午 1:03:11:System.ArgumentException:目标数组不够长。检查 destIndex 和长度,以及数组的下限。 在 System.Array.Copy(数组 sourceArray,Int32 sourceIndex,数组 destinationArray,Int32 destinationIndex,Int32 长度,布尔可靠) 在 System.Collections.Queue.Clone() 在 System.Windows.Forms.SendKeys.Send(字符串键,控制控件,布尔等待) 在 System.Windows.Forms.SendKeys.SendWait(字符串键) 在 DataViews_SendKeys.Form1.OnTimedEvent(对象源,ElapsedEventArgs e)OnTimedEvent 2011 年 4 月 19 日下午 1:04:00:System.AccessViolationException:试图读取或写入受保护的内存。这通常表明其他内存已损坏。 在 System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG 和 msg,HandleRef hwnd,Int32 msgMin,Int32 msgMax,Int32 删除) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.SendKeys.Flush() 在 System.Windows.Forms.SendKeys.Send(字符串键,控制控件,布尔等待) 在 System.Windows.Forms.SendKeys.SendWait(字符串键) 在 DataViews_SendKeys.Form1.OnTimedEvent(对象源,ElapsedEventArgs e)OnTimedEvent

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

【问题讨论】:

  • 服务器上是否打开了交互式 gui 会话?通常,服务器“没有监视器”运行,我可以想象 GetForegroundWindow();方法失败。

标签: c# windows-xp sendkeys invalidoperationexception


【解决方案1】:

这可能是由于在您的服务器上启用了 UAC 而不是在您的桌面上启用了 UAC。我在使用 SendKeys.Send() 时遇到了同样的行为

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2022-06-15
    • 2019-12-27
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    相关资源
    最近更新 更多