Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- lock(this)
- {
- // perform the write.
- }
- // try enter will return false if another thread owns the lock
- if (Monitor.TryEnter(lockObj))
- {
- try
- {
- // check last write time here, return if too soon; otherwise, write
- }
- finally
- {
- Monitor.Exit(lockobj);
- }
- }
- FileInfo fi = new FileInfo(path);
- if (fi.Exists
- && (DateTime.UtcNow - fi.LastWriteTimeUtc < TimeSpan.FromMinutes(15)) {
- // file is fresh
- return;
- }
- FileStream fs;
- try {
- fs = new FileStream(
- path, FileMode.Create, FileAccess.Write, FileShare.Read);
- } catch (IOException) {
- // file is locked
- return;
- }
- using (fs) {
- // write to file
- }
Add Comment
Please, Sign In to add comment