andrew4582

Mutex Helper

Aug 15th, 2010
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.43 KB | None | 0 0
  1. namespace System {
  2.     using System;
  3.     using System.Collections.Generic;
  4.     using System.Collections;
  5.     using System.ComponentModel;
  6.     using System.Data;
  7.     using System.Text;
  8.     using System.Threading;
  9.     using System.IO;
  10.     using System.Net;
  11.     using System.Diagnostics;
  12.     using System.Xml;
  13.     using System.Xml.Serialization;
  14.     using System.Configuration;
  15.     using System.Globalization;
  16.     using System.Drawing;
  17.     using System.Windows.Forms;
  18.     using System.Data.SqlClient;
  19.     using System.Data.Common;
  20.  
  21.     [Category("System")]
  22.     public static class MutexHelper {
  23.         /// <summary>
  24.         /// Time out in millisecongs
  25.         /// </summary>
  26.  
  27.         public const int MutexTimeOut = 30000;
  28.         public static void WriteAllFileText(
  29.             string fileName,
  30.             string contents,
  31.             string mutexName) {
  32.             FileTextIO(fileName, mutexName, MutexHelper.MutexTimeOut, true,false, contents);
  33.         }
  34.         public static void AppendTextToFile(
  35.             string fileName,
  36.             string contents,
  37.             string mutexName) {
  38.             FileTextIO(fileName, mutexName, MutexHelper.MutexTimeOut, true,true, contents);
  39.         }
  40.  
  41.         private static string GetMutexName(string fileName) {
  42.             return "mutexName_" + fileName.Length.ToString();
  43.         }
  44.         public static string ReadAllFileText(string fileName) {
  45.             string mutexName = fileName.Trim().ToLower();
  46.             mutexName = GetMutexName(fileName);
  47.             return ReadAllFileText(fileName, mutexName, MutexHelper.MutexTimeOut);
  48.         }
  49.         public static string ReadAllFileText(
  50.             string fileName,
  51.             string mutexName,
  52.             int timeOut) {
  53.             return FileTextIO<string>(fileName, mutexName, timeOut, false,false, null);
  54.         }
  55.         private static string FileTextIO<TFileData>(
  56.             string fileName,
  57.             string mutexName,
  58.             int timeOut,
  59.             bool write,
  60.             bool append,
  61.             TFileData data) {
  62.  
  63.             Trace.TraceInformation("Starting to open Mutex " + mutexName);
  64.             bool createdNew = false;
  65.             bool doesNotExist = false;
  66.  
  67.             Mutex mtx = null;
  68.             try {
  69.                 mtx = Mutex.OpenExisting(mutexName);
  70.             }
  71.             catch (WaitHandleCannotBeOpenedException) {
  72.                 Trace.TraceInformation("Mutex does not exist.");
  73.                 doesNotExist = true;
  74.             }
  75.             catch (AbandonedMutexException abmtxError) {
  76.                 Log.WriteEntry(abmtxError);
  77.             }
  78.             catch (Exception ex) {
  79.                 Trace.TraceError("Mutex - " + mutexName + " " + Environment.NewLine + ex.ToString());
  80.             }
  81.             if (doesNotExist)
  82.                 mtx = new Mutex(true, mutexName, out createdNew);
  83.  
  84.  
  85.             Trace.TraceInformation("Created Mutex: " + mutexName);
  86.  
  87.             try {
  88.                 Trace.TraceInformation("Starting " + mutexName + ".WaitOne()");
  89.                 if (!mtx.WaitOne())
  90.                     throw new TimeoutException();
  91.                 Trace.TraceInformation("Enter thread safe area");
  92.                 if (write) {
  93.                     if (append) {
  94.                         Trace.TraceInformation("Appending Text to " + fileName);
  95.                         File.AppendAllText(fileName, data.ToString());
  96.                         Trace.TraceInformation("Appended " + data.ToString().Length + " to " + fileName);
  97.                         return null;
  98.                     }
  99.                     else {
  100.                         Trace.TraceInformation("Starting WriteAllText");
  101.                         try {
  102.                             Trace.TraceInformation("Checking if " + fileName + "  Exists");
  103.                             if (File.Exists(fileName)) {
  104.                                 Trace.TraceInformation("Deleting " + fileName);
  105.                                 File.Delete(fileName);
  106.                             }
  107.                         }
  108.                         catch (System.IO.IOException ex) {
  109.                             Trace.TraceWarning(ex.Message);
  110.                             Trace.TraceInformation("Waiting for file to release");
  111.                             Thread.Sleep(2500);
  112.                             if (File.Exists(fileName))
  113.                                 File.Delete(fileName);
  114.                         }
  115.                         Trace.TraceInformation("Writing to " + fileName);
  116.                         File.WriteAllText(fileName, data.ToString());
  117.                         Trace.TraceInformation("Wrote " + data.ToString().Length + " to " + fileName);
  118.                         return null;
  119.                     }
  120.                 }
  121.                 else {
  122.                     Trace.TraceInformation("Stating ReadAllText");
  123.                     if (!File.Exists(fileName))
  124.                         throw new FileNotFoundException(null, fileName);
  125.                     return File.ReadAllText(fileName);
  126.                 }
  127.             }
  128.             finally {
  129.                 mtx.ReleaseMutex();
  130.                 if (createdNew) {
  131.                     Trace.TraceInformation("Owner starting WaitOne()");
  132.                     mtx.WaitOne();
  133.                     mtx.ReleaseMutex();
  134.                     mtx.Close();
  135.                 }
  136.                 Trace.TraceInformation("Released Mutex " + mutexName);
  137.             }
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment