c#: “System.InvalidOperationException: 队列为空”使用 SendKey.SendWait() 时出现和其他错误
我编写了一个程序来将“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: 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。该程序在我的笔记本电脑上运行没有错误,但在服务器计算机上则不然。有什么想法吗?
I wrote a program to send 'Enter' key to a certain active window. I used a Timer to get the title of the active window for the moment and act accordingly. I made an error log file so i can keep track of all the errors. Here's the code that's creating the error:
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");
}
}
Here's the GetActiveWindowTitle() method.
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;
}
}
Here are the errors I am receiving:
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
I should mention that, the computer I'm running this program on is a HP server computer with Intel Xeon processor and the OS is Windows XP. The program runs without an error on my laptop, but not on the server computer. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能是由于您的服务器上启用了 UAC,而不是您的桌面上启用的。我在使用 SendKeys.Send() 时遇到了相同的行为。
另一件事是要检查您正在构建的 .NET 版本。请参阅 SendKeys 发送被阻止。
This is likely caused by UAC being enabled on your server but not your desktop. I experienced the same behavior when using SendKeys.Send()
Another thing to check is the .NET version you're building against. See SendKeys Send is Blocked.