?
Solved

Controlling Simultaneous read/writes to a file

Posted on 2006-06-02
7
Medium Priority
?
445 Views
Last Modified: 2008-02-26
I want to carry a transaction number in a file shared by a number of processes.

Each process will periodically update this transaction number however I want to be able to control contention on this file.
In C or C++ you can use the Lock directive to perform an exclusive lock of a portion of the file such that if any other process attempts a simultaneous lock, the second process will be placed in CPU wait until the lock is removed by the first process.

A process will lock the file, read and increment the transaction number before writing the new transaction number and finally unlocking the file.

What directives are used in C#.NET to accomplish an exclusive lock on a file that will place any other bidding processes on hold until the first lock is removed.

Thanks.

PSernz
0
Comment
Question by:PSernz
7 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 16816494
You can use event handlers that fire when one of your processes has a "lock" then then it is "unlocked" send another event so the next write/read can take place.
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16817219
Define "process".  Are we talking literal processes with different application domains?  If so "events" obviously wont work.

If you've done it in C++ and know how then why dont you just do the same?

Use P/Invoke to call those same exactly win32 dll.

[DllImport(KERNEL32, SetLastError=true)]
public static extern bool LockFile(IntPtr handle, long offset, long count);
0
 
LVL 21

Accepted Solution

by:
mastoo earned 750 total points
ID: 16817230
A named mutex is an easy way to coordinate processes.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:devsolns
ID: 16817252
I should clarify, [DllImport("kernel32.dll", SetLastError = true)]
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16817258
"A named mutex is an easy way to coordinate processes."

-agree
0
 

Author Comment

by:PSernz
ID: 16820682
I don't want to go back to C++ if I can avoid it.

Can someone provide an example of a named mutex used to achieve a process lock.
Yes my processes are different application domains.

Thanks.
0
 
LVL 13

Expert Comment

by:devsolns
ID: 16841778
your not going "back to c++".  again you can call win32 dll from c# effortlessly.
0

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.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

850 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