asp.net - Threads in c# to be executed at 1 minute intervals of server time -
i have created thread in c# application. code given below.
[webmethod] public void start() { thread thread = new thread(new threadstart(workthreadfunction)); thread.start(); } [webmethod] public void stop() { thread thread = new thread(new threadstart(workthreadfunction)); thread.abort(); } public void workthreadfunction() { try { timezone zone = timezone.currenttimezone; datetime dt = datetime.now.addhours(12); dt = dt.addminutes(30); timespan offset = zone.getutcoffset(datetime.now); string s = "insert tb_log(timestamp) values('" + dt + "')"; class1 obj = new class1(); string res = obj.executequery(s); } catch { } }
when run code value enters @ 1 time table. need execute thread @ 1 min intervals throughout day, week , year. how make possible? correct me if code had written correct or not. i'm new threads in c#. please me out. , regards..
public webserviceclass : webservice { private boolean terminated = false; private boolean running = false; [webmethod] public void start() { if (running) { //already running! } else { running = true; terminated = false; //start new thread run @ requested interval thread thread = new thread(new threadstart(workthreadfunction)); thread.start(); } } [webmethod] public void stop() { //tell thread stop running after has completed it's current loop terminated = true; } public void workthreadfunction() { try { datetime nextloopstart = datetime.now; while (!terminated) { timezone zone = timezone.currenttimezone; datetime dt = datetime.now.addhours(12); dt = dt.addminutes(30); timespan offset = zone.getutcoffset(datetime.now); string s = "insert tb_log(timestamp) values('" + dt + "')"; class1 obj = new class1(); string res = obj.executequery(s); while (datetime.now < nextloopstart) { system.threading.thread.sleep(100); } nextloopstart += new timespan(0,1,0); } //reset terminated host class knows thread no longer running } catch (threadabortexception) { //logwarning("info: thread aborted"); } catch (exception e) { //logerror("error in execute: " + e.message); } { running = false; } } }
Comments
Post a Comment