multithreading - How to create a scheduled long running process using windows service in c# -
i want create windows service performs long , heavy work. code inside onstart method this:
protected override void onstart(string[] args) { system.io.file.writealltext( @"c:\mms\logs\winservicelogs.txt", datetime.now + "\t mms service started." ); this.requestadditionaltime(5*60*1000); this.runservice(); }
this.runservice()
sends request wcf service library hosted on iis. long processes, ranging 1-20 min, depending on data has process. service i'm writing supposed scheduled run every day in morning. far, runs , works fine, when time goes on few seconds or min, generates timeout exception. causes windows service in unstable state, , can't stop or uninstall without restarting computer. since, i'm trying create automated system, issue.
i did this.requestadditionaltime()
, i'm not sure whether it's doing it's supposed or not. don't timeout error message, don't know how schedule runs every day. if exception occurs, won't run next time. there several articles , so's found, there's i'm missing , can't understand it.
should create thread? articles shouldn't put heavy programs in onstart, should put heavy codes then? right now, when service starts, huge data processing makes windows service status "starting", , stays there long time until either program crashes due timeout, or completes successfully. how can start service, set status running
while code running data processing?
your situation might better suited scheduled task lloyd said in comments above. if want use windows service, need add/update in service code. allow service list started , not timeout on you. can adjust timer length suit needs.
private timer processingtimer; public yourservice() { initializecomponent(); //initialize timer processingtimer = new timer(60000); //set run every 60 seconds processingtimer.elapsed += processingtimer_elapsed; processingtimer.autoreset = true; processingtimer.enabled = true; } private void processingtimer_elapsed(object sender, elapsedeventargs e) { //check time if (timecheck && haventruntoday) //run code //you should still run separate thread this.runservice(); } protected override void onstart(string[] args) { //start timer processingtimer.start(); } protected override void onstop() { //check make sure code isn't still running... (if separate thread) //stop timer processingtimer.stop(); } protected override void onpause() { //stop timer processingtimer.stop(); } protected override void oncontinue() { //start timer processingtimer.start(); }
Comments
Post a Comment