none
Implementation of Reader Writer algorithm in C# RRS feed

  • Question

  • hello all,

     

    I have 1 doubt regarding reder-writer algorithm implementation in C#.

    I am having 5 functions in my application. They can occur at anytime and they are need to access one function(to write LOG in file).

    I dont want to loose any function information out of 5 functions becaze of simultaneous writing. If if one funcion is writing on LOG file, other needs o wait for the same.

    how can i implement such situation in C# application. Kindly help me for the same.

    can use for Mutex for the same. But i am confused how i use them correctly in code

     

    thanks in advance

     

    jicky

    Friday, December 31, 2010 10:22 AM

Answers

  • If what you need is regular logging take a look at log4net.

    Another, really crude, approach would be to open the file exclusively write whatever you wnat to write and close it. Whenever you fail to open the file exclusively, sleep for about .5 seconds and retry to open the file.

    As for synchronizing output to a file:

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Threading;

    using

     

    System.IO;

    namespace

     

    EasyLog

    {

     

      class Log

      {

     

      private FileStream output;

     

      private StreamWriter writer;

     

      private object synchObject = new object();

     

    public Log(string filename)

      {

        output =

    new FileStream(filename, FileMode.OpenOrCreate,FileAccess.Write);

        output.Seek(0,

    SeekOrigin.End);

        writer =

    new StreamWriter(output);

      }

     

      public void WriteLine(string message)

      {

     

        lock (synchObject)

        {

          writer.WriteLine(message);

          writer.Flush();

        }

      }

     

      }

    }


    Espen Harlinn
    • Proposed as answer by eryang Tuesday, January 4, 2011 3:28 AM
    • Marked as answer by eryang Wednesday, January 5, 2011 3:54 AM
    Friday, December 31, 2010 10:57 AM

All replies

  • If what you need is regular logging take a look at log4net.

    Another, really crude, approach would be to open the file exclusively write whatever you wnat to write and close it. Whenever you fail to open the file exclusively, sleep for about .5 seconds and retry to open the file.

    As for synchronizing output to a file:

    using

     

    System;

    using

     

    System.Collections.Generic;

    using

     

    System.Threading;

    using

     

    System.IO;

    namespace

     

    EasyLog

    {

     

      class Log

      {

     

      private FileStream output;

     

      private StreamWriter writer;

     

      private object synchObject = new object();

     

    public Log(string filename)

      {

        output =

    new FileStream(filename, FileMode.OpenOrCreate,FileAccess.Write);

        output.Seek(0,

    SeekOrigin.End);

        writer =

    new StreamWriter(output);

      }

     

      public void WriteLine(string message)

      {

     

        lock (synchObject)

        {

          writer.WriteLine(message);

          writer.Flush();

        }

      }

     

      }

    }


    Espen Harlinn
    • Proposed as answer by eryang Tuesday, January 4, 2011 3:28 AM
    • Marked as answer by eryang Wednesday, January 5, 2011 3:54 AM
    Friday, December 31, 2010 10:57 AM
  •  

    We temporarily mark a reply since the thread idle for a long time, please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 5, 2011 3:54 AM