gpt4 book ai didi

c#: "System.InvalidOperationException: Queue empty"和使用 SendKey.SendWait() 时的其他错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:41 26 4
gpt4 key购买 nike

我编写了一个程序来将“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)  OnTimedEvent4/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)  OnTimedEvent4/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。该程序在我的笔记本电脑上运行没有错误,但在服务器计算机上却没有。有什么想法吗?

最佳答案

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

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

关于c#: "System.InvalidOperationException: Queue empty"和使用 SendKey.SendWait() 时的其他错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com