c# - How to get keydown/press event in windows service? -
i want develop windows service detect pressed key or start when key pressed. possible?
i have created service runs after period of time , update database table.
here code have done update database after time 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 @ : " + 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 not meant ui interaction. interact differently kernel in comparison normal windows (winforms). i.e. messagebox.show("something")
won't produce inside windows service.
you need keyboard hooks.
design keyboard hook in c#. find plenty on google. place inside windows service onstart. , deploy service.
some c# keyboard hooks:
so these trap key pressed. , can check, key pressed , perform action. don't need timer control.
besides, bad idea perform task upon press of key inside windows service, because might run logic again , again, accidently pressing again , again.
what should is, deploy code in windows service. , invoke through windows task scheduler.
this how can schedule task
Comments
Post a Comment