c# - threading in Filewatcher window service -
i have written window service uses filewatcher monitor creation of file in particular folder.however,my question in real environment,many files (approx 20-30) comes in folder in production environment.how should handle such huge data files.do need implememt threading or something.for threading,any sample code appreciated not sure how do. code below:
protected override void onstart(string[] args) { xmldocument xml = new xmldocument(); xml.load("c:\\users\\\\data.xml"); xmlnodelist xnlist = xml.selectnodes("/names/name"); foreach (xmlnode xn in xnlist) { strdir = xn["directory"].innertext; filemask = xn["filemask"].innertext; strbatfile = xn["batch"].innertext; strlog = xn["log"].innertext; } m_watcher = new filesystemwatcher(); m_watcher.filter = filemask; m_watcher.path = strdir+ "\\"; m_watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite | notifyfilters.filename | notifyfilters.directoryname; m_watcher.created += new filesystemeventhandler(oncreated); m_watcher.deleted += new filesystemeventhandler(ondeleated); m_watcher.renamed += new renamedeventhandler(onrenamed); m_watcher.enableraisingevents = true; }
handler code below:
private static void oncreated(object source, filesystemeventargs e) { log.getlogger("file created- filename :" + e.name + " @ timestamp : " + datetime.now.tostring(), strlog); string strfileext = e.name; var extension = path.getextension(strfileext).toupper(); try { if (extension == ".txt") { string mybatchfile = strbatfile; string parameterfilename = strfileext; system.diagnostics.process.start(mybatchfile); log.getlogger("file processed after executing batch: filename ->:" + e.name + " " + "batch file executed- > " + strbatfile + " @ timestamp : " + datetime.now.tostring(), strlog); } } catch (exception exception) { customexception.write(customexception.createexceptionstring(exception, strfileext)); } { m_watcher.enableraisingevents = true; } }
question2-
what best practice write in onstop() method.
i found workaround thought post it.
protected override void onstart(string[] args) { current_directory = path.getdirectoryname(assembly.getexecutingassembly().location); xmldocument xml = new xmldocument(); try { string xml_path = system.appdomain.currentdomain.basedirectory; xml.load(current_directory+"\\data.xml");//suppose myxmlstring contains "<names>...</names>" xmlnodelist xnlist = xml.selectnodes("/names/name"); foreach (xmlnode xn in xnlist) { strdir = xn["directory"].innertext; filemask = xn["filemask"].innertext; strbatfile = xn["batch"].innertext; strlog = xn["log"].innertext; } m_watcher = new filesystemwatcher(); m_watcher.filter = filemask; m_watcher.path = strdir + "\\"; m_watcher.notifyfilter = notifyfilters.lastaccess | notifyfilters.lastwrite | notifyfilters.filename | notifyfilters.directoryname; m_watcher.created += new filesystemeventhandler(oncreated); m_watcher.deleted += new filesystemeventhandler(ondeleated); m_watcher.renamed += new renamedeventhandler(onrenamed); m_watcher.enableraisingevents = true; } catch (exception exception) { customexception.write(customexception.createexceptionstring(exception.tostring())); } }
Comments
Post a Comment