?
Solved

Filestream synchronization

Posted on 2011-03-03
2
Medium Priority
?
706 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 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

743 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