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 8161227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:27:14+00:00 2026-06-06T18:27:14+00:00

Background : I have a few different threads which each need to write to

  • 0

Background : I have a few different threads which each need to write to a log file (.txt). i need a way of sharing this file between the threads but i’m not sure how to go about doing this, i’ve read into perhaps using some sort of queue, and polling this queue to push the messages into the text file and such. Or do i need to lock files and just get the messages in a queue to be written after the one infront has finished?

Currently i’m writing like so;

  fileToWrite = new System.IO.StreamWriter(DeviceManager.logPath + correctDateTimeFormat);

But i’m getting the error message;

 The process cannot access the file 'filename' because it is being used by another process.

i can only assume multiple threads are trying to access this at the same time.

Can anyone point me in the right direction as to what approach i should take with this?

  • 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-06T18:27:17+00:00Added an answer on June 6, 2026 at 6:27 pm

    Those are 2 different problems you are facing here with.

    1. Opening a file again, before it is closed – this can be done by opening with appropriate sharing flags
    2. Synchronizing access to it. This one can be done, by having some mutex to allow only 1 thread to access the file at a time. This is important, because otherwise, you might have threads write to a file simultaneously, which may cause problems, like messages crossing between self.

    This FileStream constructor with FileSharing parameter can help you with opening files from few threads, before closing them.

    However, for your need, it would be better if you would centralize access to your file (for example, log file) in one place (make a class, that have one instance which will control access to this particular file) and make sure, it will synchronize access from different threads by locking some private mutex, so you will not write to file from few threads simultaneously.

    Very simple example, which needs building up:

    class Logger : IDisposable
    {
        private FileStream file; //Only this instance have a right to own it
        private StreamWriter writer;
        private object mutex; //Mutex for synchronizing
    
        public Logger(string logPath)
        {
            file = new FileStream(logPath);
            writer = new StreamWriter(file);
            mutex = new object();
        }
    
        // Log is thread safe, it can be called from many threads
        public void Log(string message)
        {
            lock (mutex)
            {
                 writer.WriteLine(message);
            }
        }
    
        public void Dispose()
        {
              writer.Dispose(); //Will close underlying stream
        }
    }
    

    Once again, it is very simple, only to show basic rule of what we are trying to achieve here.

    Few other options you have here:

    1. Use ready to use logging libraries (Log4Net or NLog (which I prefer personally))
    2. Log may add message to a queue and you can have another thread, internal for logger reading messages from this queue and write them, this way, callers of Logger will not have to wait for a log message to finish logging
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has a few background worker, each doing different work. When I click
I have been trying different techniques while designing this application, which to me is
I have a few pages which I'd like to have the same background across,
Good Morning All, I have created a different background and a few other images
I have a few classes that perform background tasks that might raise exceptions. They
Background I have a dimension table that has a single record for each day.
Background: I have this with rollup query defined in MySQL: SELECT case TRIM(company) when
Background I have a custom collection that is binded to the datagridview this.datagridview.DataSource =
Background: I have an application with athletes, each of these athletes (Athlete model) can
I am trying to mimic something that I have seen a few different times.

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.