所以我有一个巨大的程序,并决定我应该让其中一个方法在单独的线程中运行。所以我将该方法放在一个单独的类中,并在我的表单上激活它。它似乎按照我想要的方式工作,直到它到达给我这个错误的地方:
SendKeys 无法在此应用程序内运行,因为该应用程序
不处理 Windows 消息。要么将应用程序更改为
处理消息,或使用 SendKeys.SendWait 方法。
我尝试在网上寻找答案。我想我看到了一些有关 SendKeys 如何仅在表单或其他内容中工作的内容。
谁能告诉我一种在不使用 SendKeys 的情况下模拟击键的方法,或者一种让 SendKeys 在不同的非表单线程中工作的方法?
您的控制台应用程序需要消息循环。这是通过应用 http://msdn.microsoft.com/en-us/library/6y6135ed.aspx班级。您需要致电应用程序.运行(应用程序上下文) http://msdn.microsoft.com/en-us/library/ms157901.aspx.
class MyApplicationContext : ApplicationContext
{
[STAThread]
static void Main(string[] args)
{
// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.
MyApplicationContext context = new MyApplicationContext();
// Run the application with the specific context. It will exit when
// the task completes and calls Exit().
Application.Run(context);
}
Task backgroundTask;
// This is the constructor of the ApplicationContext, we do not want to
// block here.
private MyApplicationContext()
{
backgroundTask = Task.Factory.StartNew(BackgroundTask);
backgroundTask.ContinueWith(TaskComplete);
}
// This will allow the Application.Run(context) in the main function to
// unblock.
private void TaskComplete(Task src)
{
this.ExitThread();
}
//Perform your actual work here.
private void BackgroundTask()
{
//Stuff
SendKeys.Send("{RIGHT}");
//More stuff here
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)