• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

Filestream synchronization

Hello Experts,

I have 2 threads that access the same filestream.  How do I synchronize the 2 threads so they don't interfere with each other?  also had would I synchronize if I added more threads.  I think manualResetEvent would work but I'm not quite sure.

1 Solution
käµfm³d 👽Commented:
How do they access the stream? If you are only reading, I don't believe you need synchronization.
Mike TomlinsonMiddle School Assistant TeacherCommented:
As a generic answer, you create a Shared Object that all the threads can access and then enclose all access to the FileStream in a SyncLock block:

    Public Class Sync
        Public Shared MyObject As New Object
    End Class

        SyncLock Sync.MyObject
            ... do something in here ...
        End SyncLock

As each thread hits the SyncLock block it will attempt to get exclusive access to the object.  If another thread already has it locked then the thread will stop and wait until the object has been released.  Only one thread can have a lock at any given time.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now