c# - Windows Service does not start my application -
this question has answer here:
- windows service run constantly 5 answers
long story short built console application worked fine, told needed windows service. constructed windows service project pretty following microsoft's recommendations , service runs , starts , stops fine doesn't start application converted console app service. i'm having trouble understanding why.
question: how start long running process using windows service? can windows services not utilize code in class library's?
public partial class callqservice : servicebase { private static system.threading.timer _timer; private static callq _callq; [dllimport("advapi32.dll", setlasterror = true)] private static extern bool setservicestatus(intptr handle, ref servicestatus servicestatus); public callqservice(string[] args) { // update service state start pending. servicestatus servicestatus = new servicestatus(); servicestatus.dwcurrentstate = servicestate.service_start_pending; servicestatus.dwwaithint = 100000; setservicestatus(servicehandle, ref servicestatus); initializecomponent(); string eventsourcename = "mysource"; string logname = "mynewlog"; if (args.any()) { eventsourcename = args[0]; } if (args.length > 1) { logname = args[1]; } eventlog1 = new eventlog(); if (!sourceexists(eventsourcename)) { createeventsource(eventsourcename, logname); } eventlog1.source = eventsourcename; eventlog1.log = logname; // update service state running. servicestatus.dwcurrentstate = servicestate.service_running; setservicestatus(servicehandle, ref servicestatus); } protected override void onstart(string[] args) { debugger.launch(); eventlog1.writeentry("the service has started"); system.timers.timer timer = new system.timers.timer { interval = 60000 // 60 seconds }; timer.elapsed += new elapsedeventhandler(ontimer); timer.start(); //this process starts supposed run application , generate data. not @ all. hits , runs through code in class file, once supposed move code in 1 of class library's stops , doesn't else. initialize(); } private void ontimer(object sender, elapsedeventargs e) { // todo: insert monitoring activities here. eventlog1.writeentry("monitoring system", eventlogentrytype.information); } protected override void oncontinue() { eventlog1.writeentry("in oncontinue."); // update service state running. servicestatus servicestatus = new servicestatus(); servicestatus.dwcurrentstate = servicestate.service_continue_pending; servicestatus.dwwaithint = 100000; setservicestatus(servicehandle, ref servicestatus); servicestatus.dwcurrentstate = servicestate.service_running; setservicestatus(this.servicehandle, ref servicestatus); } protected override void onpause() { // update service state start pending. servicestatus servicestatus = new servicestatus(); servicestatus.dwcurrentstate = servicestate.service_pause_pending; servicestatus.dwwaithint = 100000; setservicestatus(servicehandle, ref servicestatus); eventlog1.writeentry("the service has stopped."); // update service state running. servicestatus.dwcurrentstate = servicestate.service_paused; setservicestatus(this.servicehandle, ref servicestatus); } protected override void onstop() { // update service state start pending. servicestatus servicestatus = new servicestatus(); servicestatus.dwcurrentstate = servicestate.service_stop_pending; servicestatus.dwwaithint = 100000; setservicestatus(servicehandle, ref servicestatus); eventlog1.writeentry("the service has stopped."); // update service state running. servicestatus.dwcurrentstate = servicestate.service_stopped; setservicestatus(this.servicehandle, ref servicestatus); } public static void initialize() { var calldatarepo = createcalldatarepo(); var skillsrepo = createskillsrepo(); _callq = new callq(skillsrepo, calldatarepo); _callq.start(); setuptimer(new timespan(06, 46, 00)); } private static iskillsrepo createskillsrepo() { // generates instance of config data pulling down skills oadb database iskillsdatarepoconfig skilldatarepoconfig = new skillsdatarepoconfig(); // generates instance of skills repo , pulling in call data repo oadb databse config iskillsrepo skillsrepo = new skillsrepo(skilldatarepoconfig); return skillsrepo; } private static icalldatarepo createcalldatarepo() { // generates instance of config data pulling down skills oadb database icalldatarepoconfig calldatarepoconfig = new calldatarepoconfig(); // generates instance of call data repo , pulling in call data repo oadb databse config icalldatarepo calldatarepo = new calldatarepo(calldatarepoconfig); return calldatarepo; } private static void setuptimer(timespan alerttime) { datetime current = datetime.now; timespan timetogo = alerttime - current.timeofday; if (timetogo < timespan.zero) { return;//time passed } _timer = new system.threading.timer(x => { _callq.stopgenerators(); _callq.start(); }, null, timetogo, timeout.infinitetimespan); } }
normally if have objects should exist lifetime of service, not idea have them local variables in onstart method. go out of scope , garbage collected.
i.e., make timer member variable of class , see whether makes difference.
Comments
Post a Comment