c#sendkeys.send/sendwait访问被拒绝(winform)
我创建了一个输入模拟器,您(用户)可以创建一个命令列表,并且该应用程序将根据您声明/创建的内容执行它。这是关于鼠标和键盘输入的。
详细信息
•我使用sendkeys.send()
或sendkeys.sendwait()
用于键盘输入。
•我尝试了两种执行类型
• Upon opening the app, if you (the user) doesn't press anything in a couple of seconds, the list of commands will execute.
• Threading: The list of commands will run every hour until you stop the app or the thread.
问题
•我会在尝试执行键盘输入时间歇性地拒绝'访问权限。
方案
•任务调度程序(打开应用程序)(每1小时重复一次) 或线程(每小时执行命令)
•任务调度程序或线程的第一次运行没有错误消息,但是对于下一个重复/下一个小时,它将间歇性地触发该错误。
************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): Access is denied
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 WinformApp.clsAutomation.Start(String& ex)
添加了详细信息
•我每次发生错误时,每2分钟添加了每2分钟的命令/自动化,每次发生一次,这是同一件事,但仍会发生错误,(我将VNCViewer使用到PC上)我连接到PC(用于监视)应用程序应按照应有的运行,不会发生错误。
•我的应用程序作为管理员运行。
•我删除了窗户的睡眠/空闲时间。
•OS:Windows 10
代码部分
// Some checking are already done, and this is the part for SendKeys.SendWait
try
{
process.WaitForInputIdle();
SendKeys.SendWait(_string);
}
catch (Exception ex)
{
// Log error message
}
I created an input simulator wherein you (the user) can create a list of commands, and the application will execute it based on what you'd declared/created. It's about mouse and keyboard inputs.
DETAILS
• I use SendKeys.Send()
or SendKeys.SendWait()
for keyboard inputs.
• I tried 2 types of execution
• Upon opening the app, if you (the user) doesn't press anything in a couple of seconds, the list of commands will execute.
• Threading: The list of commands will run every hour until you stop the app or the thread.
PROBLEM
• I get an error 'Access is denied' intermittently whenever it tries to do keyboard inputs.
SCENARIO
• Task Scheduler (to open the app) (repeat every 1 hour)
or Threading (every hour execution of commands)
• The first run of task scheduler or thread it has no error message, but for the next repeat/next hour it will trigger the error intermittently.
************** Exception Text **************
System.ComponentModel.Win32Exception (0x80004005): Access is denied
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 WinformApp.clsAutomation.Start(String& ex)
ADDED DETAILS
• I added redo the commands/automation for every 2 minutes for 20 maximum tries everytime the error occurs and it's the same thing, the error still occurs, (I use VNCviewer to the PC) but everytime I connect to the pc (for monitoring) the apps run as it should be, no error occurs.
• My app runs as admin.
• I removed sleep/idle time of windows.
• OS: Windows 10
CODE PART
// Some checking are already done, and this is the part for SendKeys.SendWait
try
{
process.WaitForInputIdle();
SendKeys.SendWait(_string);
}
catch (Exception ex)
{
// Log error message
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
锁定帐户时,Windows不允许您运行键盘和鼠标仿真。
要解决这个问题,请使用 https://learlen.microsoft.com/en-en--en---en---en--- US/sysinternals/downloads/autologon 要在启动时自动登录您的帐户(它将在不锁定您的帐户的情况下执行此操作),并在每个计划执行之前重新启动计算机。
远程桌面连接默认情况下,当您结束会话时,默认情况下锁定了您的帐户,我怀疑vncviewer正在执行相同的操作。
由于您使用VNCViewer,因此在结束会话结束时可能会有一种方法可以禁用计算机的锁定,从而允许您的任务运行。
更多信息:
https://superuser.com/questions/407407/407407/vnc-sessise-winc-sessise-withith-withith-locked-screen < /a>
Windows doesn't let you run keyboard and mouse emulation when the account is locked.
To get around this, use https://learn.microsoft.com/en-us/sysinternals/downloads/autologon to automatically login to your account on boot (and it will do this without locking your account) and make a task that reboots your computer before each scheduled execution.
Remote Desktop Connection locks your account by default when you end a session and I suspect VNCViewer is doing the same.
Since you use VNCViewer, there may be a way to disable locking of your computer when you end a session, allowing your task to run.
More information:
https://superuser.com/questions/407407/vnc-session-with-locked-screen