c# - How to wait until File.Exists? -
i have app, listening *.log file in chosen folder. used filesystemwatcher
.
but there problem. other app responsible making file takes next steps:
make *.gz file unpack txt file (some random file name) change *.txt name proper 1 *.log extension.and can not alter behaviour.
so made 2 filesystemwatcher
s *.gz, , *.txt files. why? because app doesn't unpack gz file, , doesn't rename txt file final *.log file.
filesystemwatcher2
catches txt file, (in cases renamed log in next 1000ms) need wait time check if txt file exists (if not, seems renamed final *.log file).
the question is, how check if file exists without thread.sleep()
prevent ui freeze?
i hope clear, if not seek describe better. think complex problem.
some code sample:
watcher gz file:
private void filesystemwatcher_created(object sender, filesystemeventargs e) { //this gz files in case if gz file not unpacked automatically other app //i need wait , check if gz unpacked, if not, unpack myself, //then txt watcher grab thread.sleep(5000); if (file.exists(e.fullpath)) { seek { byte[] databuffer = new byte[4096]; using (system.io.stream fs = new filestream(e.fullpath, filemode.open, fileaccess.read)) { using (gzipinputstream gzipstream = new gzipinputstream(fs)) { string fnout = path.combine(path_to_watcher, path.getfilenamewithoutextension(e.fullpath)); using (filestream fsout = file.create(fnout)) { streamutils.copy(gzipstream, fsout, databuffer); } } } } grab { //ignore } } }
watcher txt file:
private void filesystemwatcher2_created(object sender, filesystemeventargs e) { //this txt file thread.sleep(3500); if (file.exists(e.fullpath)) { //make actions } else { //make actions } }
actually filesystemwatcher created event called in separate thread .net itself.. you need absolutely nothing. code ok is.
here proof:
class programme { static void main(string[] args) { filesystemwatcher fw = new filesystemwatcher(@"c:\temp"); fw.created += filesystemwatcher_created; console.writeline(thread.currentthread.managedthreadid); fw.enableraisingevents = true; console.readline(); } static void filesystemwatcher_created(object sender, filesystemeventargs e) { console.writeline(thread.currentthread.managedthreadid); } }
c# multithreading file sleep exists
No comments:
Post a Comment