2017-04-26 1 views
0

J'essaie d'utiliser Mutex pour gérer différents processus/application à écrire sur le même fichier.Comment utiliser nommé Mutex

C'est mon code

ILogTest logTest = new LogTest(new FileLog()); 
logTest.PerformanceTest(); 


    public class ILogTest 
    { 
      public void PerformanceTest() 
      { 
       for (int i = 0; i < this.numberOfIterations; i++) 
       { 
        try 
        { 
         Thread threadC = Thread.CurrentThread; 
         threadC = new Thread(ThreadProc); 
         threadC.Name = i.ToString(); 
         threadC.Start(); 
         threadC.Suspend(); 
         threadC.IsBackground = true; 
        } 
        catch (Exception) 
        { 
         throw new Exception("errore"); 
        } 
       } 
      } 

      private void ThreadProc() 
      { 
       try 
       { 
        log.Write("Thread : " + Thread.CurrentThread.Name.ToString()); 
        this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString()); 
        this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString()); 
        this.log.Write("Thread : " + Thread.CurrentThread.Name.ToString()); 
       } 
       catch (Exception) 
       { 
        throw new Exception("errore"); 
       } 
      } 
    } 

FileLog est une implémentation de ILogTest.

méthode Write:

public void Write(string message) 
    { 
     try 
     { 
      rwl.WaitOne(); 
      try 
      { 
       string tID = Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.CurrentCulture); 

       sw.WriteLine(sev.ToString() + "\t" + DateTime.Now.ToString("dd.MM.yyyy hh:mm:ss", CultureInfo.CurrentCulture) + "\t\t" + System.Reflection.Assembly.GetCallingAssembly().GetName().Name + "\t" + Process.GetCurrentProcess().Id.ToString(CultureInfo.CurrentCulture) + "\t" + tID + " \t " + message); 


       sw.WriteLine("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"); 
      } 
      catch (Exception ex) 
      { 
       throw new ArgumentException("Cannot write to file " + ex.Message); 
      } 
      finally 
      { 
       rwl.ReleaseMutex(); 
      } 
     } 
     catch (ApplicationException) 
     { 
     } 
    } 

FileLog principale:

public FileLog() 
    { 
     try 
     { 
      rwl.WaitOne(); 
      string filePath = Path.GetTempPath() + "Test.txt"; 
      if (!File.Exists(filePath)) 
      { 
       swa = new FileStream(filePath, FileMode.Append, FileAccess.Write); 
       sw = new StreamWriter(swa); 
      } 
     } 
     catch (Exception ex) 
     { 
      throw new ArgumentException("Cannot open or create file " + ex.Message); 
     } 

     try 
     { 
      if (sw == null) 
      { 
       swa = new FileStream(filePath, FileMode.Append, FileAccess.Write); 
       sw = new StreamWriter(swa); 
      } 

      sw.AutoFlush = true; 
     } 
     catch (Exception ex) 
     { 
      throw new ArgumentException("Cannot write to file " + ex.Message); 
     } 
    } 

Essayer de simuler, il n'écrit rien à déposer, créer seulement il .. Je ne sais pas pourquoi. Quelqu'un peut-il m'aider? grâce

+0

Le verrouillage de fichier ne serait-il pas meilleur? https://msdn.microsoft.com/en-us/library/system.io.filestream.lock(v=vs.110).aspx –

+0

@AdamBenson Cela nécessiterait tous les autres threads/processus qui tentent d'écrire simultanément pour attraper et gérer le 'IOException' qui est levée lorsque le fichier est actuellement verrouillé. Selon l'affirmation qui pourrait ne pas être idéal. –

+0

Envisagez d'utiliser FileAppender de log4net avec le modèle de verrouillage [InterProcessLock] (https://logging.apache.org/log4net/release/sdk/html/T_log4net_Appender_FileAppender_InterProcessLock.htm). Ne réinventez pas la roue si vous n'êtes pas obligé de le faire. (Vous pouvez au moins utiliser [leur implémentation] (https://github.com/apache/log4net/blob/fa13c71e628fd0a59880ecb50e79dc0d5724fa39/src/Appender/FileAppender.cs#L723) pour voir comment cela peut être fait.) –

Répondre

1

Le constructeur appelle FileLogrwl.WaitOne() qui acquiert le mutex (je suppose rwl est un objet Mutex), mais jamais appelle rwl.ReleaseMutex().

Ainsi, chaque thread qui appelle Write() bloque sur son appel à rwl.WaitOne() car le mutex appartient toujours à un autre thread.

Vous devrez libérer le Mutex dans le constructeur FileLog afin que les autres threads puissent l'acquérir. De plus, vous avez un appel à threadC.Suspend(); dans PerformanceTest; à moins qu'il y ait du code qui reprend les threads, ils ne finiront jamais de tourner.