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

  • SEARCH
  • Home
  • 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 8797271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:49:14+00:00 2026-06-13T23:49:14+00:00

I have a process that needs to read and write to a file. The

  • 0

I have a process that needs to read and write to a file. The application has a specific order to its reads and writes and I want to preserve this order. What I would like to do is implement something that lets the first operation start and makes the second operation wait until the first is done with a first come first served like of queue to access the file. From what I have read file locking seems like it might be what I am looking for but I have not been able to find a very good example. Can anyone provide one?

Currently I am using a TextReader/Writer with .Synchronized but this is not doing what I hoped it would.

Sorry if this is a very basic question, threading gives me a headache :S

  • 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-06-13T23:49:15+00:00Added an answer on June 13, 2026 at 11:49 pm

    It should be as simple as this:

    public static readonly object LockObj = new object();
    
    public void AnOperation()
    {
        lock (LockObj)
        {
            using (var fs = File.Open("yourfile.bin"))
            {
                // do something with file
            }
        }
    }
    
    public void SomeOperation()
    {
        lock (LockObj)
        {
            using (var fs = File.Open("yourfile.bin"))
            {
                // do something else with file
            }
        }
    }
    

    Basically, define a lock object, then whenever you need to do something with your file, make sure you get a lock using the C# lock keyword. On reaching the lock statement, execution will block indefinitely until a lock has been obtained.

    There are other constructs you can use for locking, but I find the lock keyword to be the most straightforward.

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

Sidebar

Related Questions

I have a C++ process which has a thread that needs to send floats
I have a multi-process python application (processes are spawned by uwsgi) that needs to
I have a timer that needs to not process its elapsed event handler at
I have written a Scala (2.9.1-1) application that needs to process several million rows
i have written an application that needs to process outgoing calls. Everything works fine,
Background I have a Spring batch program that reads a file (example file I
I have a file that is read and written to. I need to make
I have an application that logs information to a daily text file every second
I have a process that will be called rather frequently from cron to read
Summary: I have a struct that is read/written to file. This struct changes frequently,

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.