c# - Call a method in a specific time of the day -
i want call method in specific time of day without need request page :
update: done following class , without task schedule or else
something windows schedule
i did in class:
public class class1 { private const string dummycacheitemkey = "gagagugugigi"; protected void application_start(object sender, eventargs e) { var result = registercacheentry(); if (!result) { debug.writeline("the dummycacheitem alive!"); } } public bool registercacheentry() { if (null != httpcontext.current.cache[dummycacheitemkey]) return false; try { httpcontext.current.cache.add(dummycacheitemkey, "test", null, datetime.maxvalue, timespan.fromminutes(1), cacheitempriority.normal, new cacheitemremovedcallback(cacheitemremovedcallback)); }catch( exception ex) { debug.writeline("exeption error: " + ex); } return true; } public void cacheitemremovedcallback(string key, object value, cacheitemremovedreason reason) { debug.writeline("cache item callback: " + datetime.now.tostring() + " removed!"); try { hitpage(); }catch(exception ex) { debug.writeline("hitpage unsuccessful: " + ex); } // service works dowork(); //sendmail(); } private const string dummypageurl = "http://localhost:53509/page.cshtml"; private void hitpage() { webclient client = new webclient(); client.downloaddata(dummypageurl); } protected void application_beginrequest(object sender, eventargs e) { // if dummy page hit, means want add item // in cache if (httpcontext.current.request.url.tostring() == dummypageurl) { // add item in cache , when succesful, work. registercacheentry(); } } private void dowork() { debug.writeline("begin dowork..."); debug.writeline("running as: " + windowsidentity.getcurrent().name); dosomefilewritingstuff(); debug.writeline("end dowork..."); } private void dosomefilewritingstuff() { debug.writeline("writing file..."); try { using (streamwriter writer = new streamwriter(@"c:\temp\cachecallback.txt", true)) { writer.writeline("cache callback: {0}", datetime.now); writer.close(); } } catch (exception x) { debug.writeline("error: " + x); } debug.writeline("file write successful"); } }
and here explanation of why did this?
is there simpler way it?
use "task scheduler" run programme in specific time.
find typing "task scheduler" in start menu.
Comments
Post a Comment