using binarywriter.write with multiple threads

My application has multiple threads that need to send byte arrays at independant times. Will i run into problems if two seperate threads call the same method containing a binarywriter.write statement. Also i have two seperate methods that write to the stream. What happens if they are execute simultaneously. I didnt see anything about the write method being threadsafe.

sample code:

//called by multiple threads

public int SendProfile(byte[] outgoingArray)

{

...

writeMessage.Write(outgoingArray);

...

}

//sent every second

public int SendStatus(Int32 i_p_MessageID, Int32 i_p_SeqNum, Int32 i_p_SD_Status,Int32 i_p_SW_Status,Int32 i_p_SG_Status,string stripID)

{

...

writeMessage.Write(bSystemStatusArray);

...

}




Answer this question

using binarywriter.write with multiple threads

  • Jan Byvaly

    BinaryWriter instance members (like Write) are not guarenteed to be thread-safe.

    You should use the lock statement to ensure only one thread at a time is using the BinaryWriter.



  • using binarywriter.write with multiple threads