Solved

Filestream synchronization

Posted on 2011-03-03
2
690 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
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 85

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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