multithreading - C# - Windows Service, thread, timer - not run -
this code windows service...but unfortunately, method mytimer_elapsed() invoked when @ least 60 sec. after starting service. why not start after switching service?
public partial class myservice : servicebase { private system.threading.thread myworkingthread; private system.timers.timer mytimer = new system.timers.timer(); protected override void onstart(string[] args) { myworkingthread = new system.threading.thread(preparetask); myworkingthread.start(); } private void preparetask() { mytimer.elapsed += new system.timers.elapsedeventhandler(mytimer_elapsed); mytimer.interval = 60000; mytimer.start(); system.threading.thread.sleep(system.threading.timeout.infinite); } void mytimer_elapsed(object sender, system.timers.elapsedeventargs e) { ..my code prepare binary db files binarydb rdr = new binarydb(); rdr.readfile(...) } }
it because timer interval set 1 minute. see below code in preparetask
mytimer.interval = 60000;
you can set appropriate value. or if want code return once @ start , after every 60 seconds should call once in onstart
method. like
protected override void onstart(string[] args) { // other initialization stuff... evalutatechangeconditions (); // create thread , tell executed. myworkingthread = new system.threading.thread(preparetask); // start thread. myworkingthread.start(); }
Comments
Post a Comment