windows xp - c#: "System.InvalidOperationException: Que

本文关键字:System InvalidOperationException Que quot xp windows | 更新日期: 2023-09-27 17:51:21

我编写了一个程序,将'Enter'键发送到某个活动窗口。我使用计时器来获取当前活动窗口的标题并采取相应的行动。我做了一个错误日志文件,这样我可以跟踪所有的错误。下面是产生错误的代码:

    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:系统。InvalidOperationException:队列空。在System.Collections.Queue.Dequeue ()在System.Windows.Forms.SendKeys。SendInput(Byte[] oldKeyboardState, Queue previsevents)在System.Windows.Forms.SendKeys。发送(字符串键,控制,布尔值等)在System.Windows.Forms.SendKeys。SendWait (String键)在DataViews_SendKeys.Form1。OnTimedEvent(对象源,ElapsedEventArgs e4/19/2011 1:03:11 PM:系统。ArgumentException:目标数组不够长。检查destIndex和length,以及数组的下界。在System.Array。复制(数组sourceArray, Int32 sourceIndex,数组destinationArray, Int32 destinationIndex, Int32长度,布尔可靠)在System.Collections.Queue.Clone ()在System.Windows.Forms.SendKeys。发送(字符串键,控制,布尔值等)在System.Windows.Forms.SendKeys。SendWait (String键)在DataViews_SendKeys.Form1。OnTimedEvent(对象源,ElapsedEventArgs e2011年4月19日下午1:04:00:系统。AccessViolationException:试图读写受保护的内存。这通常表明其他内存已损坏。在System.Windows.Forms.UnsafeNativeMethods。PeekMessage(msg & msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove)在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager。fpushmessagelloop (Int32 dwComponentID, Int32 reason, Int32 pvLoopData)在System.Windows.Forms.Application.ThreadContext。RunMessageLoopInner(Int32 reason, ApplicationContext context)在System.Windows.Forms.Application.ThreadContext。runmessagelloop (Int32 reason, ApplicationContext context)在System.Windows.Forms.SendKeys.Flush ()在System.Windows.Forms.SendKeys。发送(字符串键,控制,布尔值等)在System.Windows.Forms.SendKeys。SendWait (String键)在DataViews_SendKeys.Form1。OnTimedEvent(对象源,ElapsedEventArgs e之前我应该提一下,我运行这个程序的电脑是一台带有英特尔至强处理器的惠普服务器电脑,操作系统是Windows XP。这个程序在我的笔记本电脑上运行没有错误,但在服务器电脑上却没有。任何想法吗?

windows xp - c#: "System.InvalidOperationException: Que

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

时遇到了相同的行为

另一件要检查的事情是你正在构建的。net版本。