Solved

Filestream synchronization

Posted on 2011-03-03
2
701 Views
Last Modified: 2012-08-14
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.

Thanks,
OFGemini
0
Comment
Question by:OFGemini
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35027842
How do they access the stream? If you are only reading, I don't believe you need synchronization.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35028207
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:
http://msdn.microsoft.com/en-us/library/3a86s51t(VS.71).aspx

    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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question