Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 406161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:28:10+00:00 2026-05-12T17:28:10+00:00

I have a file that is read and written to. I need to make

  • 0

I have a file that is read and written to. I need to make sure when its been written to, nobody else will try to write to it.

I put a lock on the whole function which allows to either read or write but I still get errors such as
The process cannot access the file ‘FILENAME’ because it is being used by another process.

public static TYPE Property{

get{
    data mydata;
    Object obj = new object();
    Monitor.Enter(obj);

    // check if data has not changed
    // if it has not, just read

    using (Stream stream = File.Open(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
        //....
    }
    // else, if data changed, then need to write to  file to save the new data

    using (Stream stream = File.Open(fileLocation, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
        BinaryFormatter bf = new BinaryFormatter();

        try {
            bf.Serialize(stream, (data);
        }
        //DONE processing

        Monitor.Pulse(obj);
        Monitor.Exit(obj);
        return data
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:28:10+00:00Added an answer on May 12, 2026 at 5:28 pm

    You’re creating a new monitor to lock on every time the property is invoked. You need to lock on the same monitor otherwise there’s no point in locking at all.

    You should also just use a “lock” statement – you’re never waiting, so there’s no point in pulsing. Currently, if any exceptions are thrown, you’ll end up “leaking” the lock. That would normally be a really bad problem, but as you’re not reusing the lock anyway, that’s masking the issue.

    For example:

    private static readonly object monitor = new object();
    
    public static TYPE Property
    {
        get
        {
            lock(monitor)
            {
                // check if data has not changed
                // if it has not, just read
                using (Stream stream = File.Open(fileLocation, 
                       FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    ....
                }
                // else, if data changed, then need to write to 
                // file to save the new data
                using (Stream stream = File.Open
                           (fileLocation, FileMode.OpenOrCreate,
                            FileAccess.ReadWrite, FileShare.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(stream, data);
                }
                return data;
            }
        }
    }
    

    As an aside, this looks like it’s doing more work than I’d really expect in a property. Are you sure a method wouldn’t make more sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to read/write an XML file that stores its data as bit masked
I have a text data file that I likely will read with readLines .
I have one file, and I need everything that is written in some time
I have a file that I want to read and write to a binary
Summary: I have a struct that is read/written to file. This struct changes frequently,
I have a config file that I read using the RawConfigParser in the standard
I have some configuration data in a config file that I read off disk
I have a text file. I want read that file. But In that if
I have an XML file that I'm trying to read from here , and
I have semicolon-separated .dat file and I want to read that file and store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.