Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: how can i get this to work in c# Pin
Member 117127539-Feb-16 5:36
Member 117127539-Feb-16 5:36 
AnswerRe: how can i get this to work in c# Pin
Gerry Schmitz8-Feb-16 6:16
mveGerry Schmitz8-Feb-16 6:16 
QuestionHow To Start Process On Remote Machine which is tied to Particular service using C#.net WMI Pin
Tej_dev5-Feb-16 10:04
Tej_dev5-Feb-16 10:04 
AnswerRe: How To Start Process On Remote Machine which is tied to Particular service using C#.net WMI Pin
Dave Kreskowiak5-Feb-16 11:39
mveDave Kreskowiak5-Feb-16 11:39 
GeneralRe: How To Start Process On Remote Machine which is tied to Particular service using C#.net WMI Pin
Tej_dev8-Feb-16 5:10
Tej_dev8-Feb-16 5:10 
GeneralRe: How To Start Process On Remote Machine which is tied to Particular service using C#.net WMI Pin
Dave Kreskowiak8-Feb-16 8:50
mveDave Kreskowiak8-Feb-16 8:50 
GeneralRe: How To Start Process On Remote Machine which is tied to Particular service using C#.net WMI Pin
Tej_dev8-Feb-16 10:16
Tej_dev8-Feb-16 10:16 
QuestionDoes this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616005-Feb-16 0:15
Member 120616005-Feb-16 0:15 
I have a wrapper class around serial port which looks something like this:

C#
static class HASPCLass
   {
     private static SerialPort m_port;
     private static bool m_initialized;
     private static int m_baudRate;
     static readonly object _syncObject = new object();
     public DoInitialization(int baudRate /*also could be other params*/)
     {
       lock(_syncObject)
       {
          if (!m_initialized)
          {
            Initialize(baudRate);
          }
       }
     }

     private Initialize(int baudrate /*also could have other params*/)
     {
        m_port.open(..);
        m_baudRate = baudRate;
        m_initialized = true;
     }

     private Uninitialize()
     {
        m_port.close();
        m_initialized = false;
     }



     public void Read(byte[] buff)
     {
       lock(_syncObject)
       {
         //Other custom read stuff
         m_port.Read(buff);
       }
     }

     public void Write(byte [] buff)
     {
       lock(_syncObject)
       {
         //Other write related code
         m_port.Write(buff);
       }
     }

     public void Close()
     {
       lock(_syncObject)
       {
          if (m_initialized)
          {
            Uninitialize();
          }
       }
     }
   }

I tried making this class thread safe. Someone initializes it - read and writes maybe used from other threads - and in the end calls Close.

Now Imagine I have two additional static methods from other class which do something like this:

C#
public static void function1()
   {
    HASPClass.Read(...);

    // Some other code

    HASPClass.Write(...);
   }

   public static void function2()
   {
    HASPClass.Read(...);

    // Some other code

    HASPClass.Write(...);
   }

For overall thread safety I also enclosed these functions in locks:

C#
public static void function1()
  {
     lock(otherlock1)
     {
       HASPClass.Read(...);

       // Some other code

       HASPClass.Write(...);
      }
  }

  public static void function2()
  {
     lock(otherlock1)
     {
        HASPClass.Read(...);

        // Some other code

        HASPClass.Write(...);
      }
  }

Because order in which read and writes are called might be relavant for the serial port/HASP.

My question is: is now my final approach (of using function1 and function2) correct/thread safe? Can I encounter some problems related to threading using function1 and function2?

modified 5-Feb-16 8:08am.

AnswerRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Eddy Vluggen5-Feb-16 7:16
professionalEddy Vluggen5-Feb-16 7:16 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616005-Feb-16 7:43
Member 120616005-Feb-16 7:43 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Eddy Vluggen5-Feb-16 9:44
professionalEddy Vluggen5-Feb-16 9:44 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616005-Feb-16 10:12
Member 120616005-Feb-16 10:12 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Eddy Vluggen5-Feb-16 11:16
professionalEddy Vluggen5-Feb-16 11:16 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616005-Feb-16 11:27
Member 120616005-Feb-16 11:27 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Eddy Vluggen5-Feb-16 23:05
professionalEddy Vluggen5-Feb-16 23:05 
QuestionRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Gerry Schmitz5-Feb-16 12:50
mveGerry Schmitz5-Feb-16 12:50 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
PIEBALDconsult5-Feb-16 15:00
mvePIEBALDconsult5-Feb-16 15:00 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Gerry Schmitz5-Feb-16 15:50
mveGerry Schmitz5-Feb-16 15:50 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
PIEBALDconsult5-Feb-16 15:53
mvePIEBALDconsult5-Feb-16 15:53 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Gerry Schmitz5-Feb-16 16:13
mveGerry Schmitz5-Feb-16 16:13 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
PIEBALDconsult5-Feb-16 16:35
mvePIEBALDconsult5-Feb-16 16:35 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Gerry Schmitz5-Feb-16 16:41
mveGerry Schmitz5-Feb-16 16:41 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
PIEBALDconsult5-Feb-16 16:45
mvePIEBALDconsult5-Feb-16 16:45 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Member 120616005-Feb-16 21:57
Member 120616005-Feb-16 21:57 
GeneralRe: Does this ensure function1 and function2 use my class in thread safe way? Pin
Gerry Schmitz6-Feb-16 6:05
mveGerry Schmitz6-Feb-16 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.