Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace System {
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Diagnostics;
- using System.Xml;
- using System.Xml.Serialization;
- using System.Configuration;
- using System.Globalization;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using System.Data.Common;
- [Category("System")]
- public static class MutexHelper {
- /// <summary>
- /// Time out in millisecongs
- /// </summary>
- public const int MutexTimeOut = 30000;
- public static void WriteAllFileText(
- string fileName,
- string contents,
- string mutexName) {
- FileTextIO(fileName, mutexName, MutexHelper.MutexTimeOut, true,false, contents);
- }
- public static void AppendTextToFile(
- string fileName,
- string contents,
- string mutexName) {
- FileTextIO(fileName, mutexName, MutexHelper.MutexTimeOut, true,true, contents);
- }
- private static string GetMutexName(string fileName) {
- return "mutexName_" + fileName.Length.ToString();
- }
- public static string ReadAllFileText(string fileName) {
- string mutexName = fileName.Trim().ToLower();
- mutexName = GetMutexName(fileName);
- return ReadAllFileText(fileName, mutexName, MutexHelper.MutexTimeOut);
- }
- public static string ReadAllFileText(
- string fileName,
- string mutexName,
- int timeOut) {
- return FileTextIO<string>(fileName, mutexName, timeOut, false,false, null);
- }
- private static string FileTextIO<TFileData>(
- string fileName,
- string mutexName,
- int timeOut,
- bool write,
- bool append,
- TFileData data) {
- Trace.TraceInformation("Starting to open Mutex " + mutexName);
- bool createdNew = false;
- bool doesNotExist = false;
- Mutex mtx = null;
- try {
- mtx = Mutex.OpenExisting(mutexName);
- }
- catch (WaitHandleCannotBeOpenedException) {
- Trace.TraceInformation("Mutex does not exist.");
- doesNotExist = true;
- }
- catch (AbandonedMutexException abmtxError) {
- Log.WriteEntry(abmtxError);
- }
- catch (Exception ex) {
- Trace.TraceError("Mutex - " + mutexName + " " + Environment.NewLine + ex.ToString());
- }
- if (doesNotExist)
- mtx = new Mutex(true, mutexName, out createdNew);
- Trace.TraceInformation("Created Mutex: " + mutexName);
- try {
- Trace.TraceInformation("Starting " + mutexName + ".WaitOne()");
- if (!mtx.WaitOne())
- throw new TimeoutException();
- Trace.TraceInformation("Enter thread safe area");
- if (write) {
- if (append) {
- Trace.TraceInformation("Appending Text to " + fileName);
- File.AppendAllText(fileName, data.ToString());
- Trace.TraceInformation("Appended " + data.ToString().Length + " to " + fileName);
- return null;
- }
- else {
- Trace.TraceInformation("Starting WriteAllText");
- try {
- Trace.TraceInformation("Checking if " + fileName + " Exists");
- if (File.Exists(fileName)) {
- Trace.TraceInformation("Deleting " + fileName);
- File.Delete(fileName);
- }
- }
- catch (System.IO.IOException ex) {
- Trace.TraceWarning(ex.Message);
- Trace.TraceInformation("Waiting for file to release");
- Thread.Sleep(2500);
- if (File.Exists(fileName))
- File.Delete(fileName);
- }
- Trace.TraceInformation("Writing to " + fileName);
- File.WriteAllText(fileName, data.ToString());
- Trace.TraceInformation("Wrote " + data.ToString().Length + " to " + fileName);
- return null;
- }
- }
- else {
- Trace.TraceInformation("Stating ReadAllText");
- if (!File.Exists(fileName))
- throw new FileNotFoundException(null, fileName);
- return File.ReadAllText(fileName);
- }
- }
- finally {
- mtx.ReleaseMutex();
- if (createdNew) {
- Trace.TraceInformation("Owner starting WaitOne()");
- mtx.WaitOne();
- mtx.ReleaseMutex();
- mtx.Close();
- }
- Trace.TraceInformation("Released Mutex " + mutexName);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment