如何获得的keydown / preSS在窗口服务的事件吗? [英] How to get keydown/press event in windows service?

查看:91
本文介绍了如何获得的keydown / preSS在窗口服务的事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Windows服务,将检测pressed键或当关键是pssed $ P $将开始。这可能吗?

我创建了一段时间后,运行的服务和更新一些数据库表。

下面是code我做了什么时不时后更新数据库。

  System.Timers.Timer定时器1 =新System.Timers.Timer();
私人无效的Ini​​tializeComponent()
{
    ((System.ComponentModel.ISupportInitialize)(定时器1))BeginInit在()。
    timer1.Enabled = TRUE;
    ((System.ComponentModel.ISupportInitialize)(定时器1))EndInit在()。
}保护覆盖无效的OnStart(字串[] args)
{
    尝试
    {
        WRITELOG(测试服务开始时间:+ System.DateTime.Now);
        //经过时间事件
        timer1.Elapsed + =新ElapsedEventHandler(OnElapsedTime);
        INT intOnElapsedTime = Convert.ToInt32(
            System.Configuration.ConfigurationManager
            .AppSettings [intOnElapsedTime]
            的ToString());
        timer1.Interval = 1000 * 10 * intOnElapsedTime;
        timer1.Enabled = TRUE;
    }
    赶上(异常前)
    {
        WriteErrorLog(ex.Message,ex.StackTrace的OnStart);
    }
}私人无效OnElapsedTime(对象发件人,ElapsedEventArgs E)
{
    尝试{/ *更新数据库表* /}
    赶上(例外EXP){...}
}


解决方案

Windows服务并不意味着UI交互。他们与内核交互不同在比较正常的Windows(的WinForms)。即 MessageBox.Show(东西)不会产生一个Windows服务里的任何东西。

你需要考虑的键盘钩子的。

设计在C#键盘钩子。你会发现很多的谷歌。将其放置在Windows服务的OnStart内。和部署服务。

有一些很好的C#键盘钩子:

因此​​,这些将帮助你捕获键pressed。然后你就可以很容易地检查,有什么关键是pressed并执行你的行动。你甚至不需要定时器控制。

此外,它是执行在一个Windows服务中的一个关键的preSS一些任务,因为你会一次又一次地运行你的逻辑,由意外连连pressing一个非常糟糕的主意。

你应该做的是,在Windows服务部署code。并调用它通过Windows任务计划程序。

是你如何安排任务

I want to develop a windows service which will detect the pressed key or it will start when a key is pressed. is that possible?

I have created a service which runs after a period of time and update some database table.

Here is the code what I have done to update database after time to time.

System.Timers.Timer timer1 = new System.Timers.Timer();
private void InitializeComponent()
{
    ((System.ComponentModel.ISupportInitialize)(timer1)).BeginInit();
    timer1.Enabled = true;         
    ((System.ComponentModel.ISupportInitialize)(timer1)).EndInit();
}

protected override void OnStart(string[] args)
{
    try
    {      
        WriteLog("test Services Started at : " + System.DateTime.Now);
        // Time Elapsed event 
        timer1.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        int intOnElapsedTime = Convert.ToInt32(
            System.Configuration.ConfigurationManager
            .AppSettings["intOnElapsedTime"]
            .ToString());
        timer1.Interval = 1000 * 10 * intOnElapsedTime;
        timer1.Enabled = true;
    }
    catch (Exception ex)
    {
        WriteErrorLog(ex.Message, ex.StackTrace, "OnStart");
    }
}

private void OnElapsedTime(object sender, ElapsedEventArgs e)
{
    try{ /* update database table */}
    catch (Exception exp){ ...}
}

解决方案

Windows Service are not meant for UI Interaction. They interact differently with kernel in comparison to normal windows (winforms). i.e. MessageBox.Show("something") won't produce anything inside a Windows Service.

you need to look into Keyboard hooks.

design a keyboard hook in C#. you will find plenty on the google. place it inside the Windows Service OnStart. and deploy your Service.

Some good C# keyBoard Hooks:

So these will help you to trap the key pressed. And then you can easily check, what key is pressed and perform your action. You don't even need the timer control.

Besides, it is a very bad idea to perform some task upon press of a key inside a windows service, because you might run your logic again and again, by accidently pressing again and again.

What you should do is, to deploy your code in a windows Service. and Invoke it through Windows Task Scheduler.

This is how you can schedule a Task

这篇关于如何获得的keydown / preSS在窗口服务的事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆