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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:12:14+00:00 2026-05-16T08:12:14+00:00

I write my debug information to a file using a separate thread. During startup,

  • 0

I write my debug information to a file using a separate thread. During startup, I like to backup any previous file. Unfortunately, it seems the OS hangs onto the file handle for an indeterminate amount of time so when I try to write to the file, it fails.

I am using C#, .Net framework 3.5 on Windows XP. (Vista and Win7 have the same problem).

Here is the code that distills the problem, where t will throw a System.IO.IOException: “The process cannot access the file ‘C:\deleteMe.txt’ because it is being used by another process.”

public class WriteToFile {

    static void Main(){
        String filename=@"C:\deleteMe.txt";
        String filenameBackup = @"C:\deleteMe (backup).txt";
        String value = "this is a test value";

        //MAKE FILE
        fillFile(filename, value);


        //MAKE A THREAD TO WRITE TO FILE, WHEN READY
        Semaphore readyToWrite=new Semaphore(1, 1);
        var t=new Thread(
            new ThreadStart(delegate(){
                readyToWrite.WaitOne();
                WriteToFile.fillFile(filename, value);
            })
        );
        t.Priority=ThreadPriority.Highest;
        t.Start();


        //BACKUP FILE 
        if (File.Exists(filename)) {
            File.Delete(filenameBackup);
            File.Copy(filename, filenameBackup);
            File.Delete(filename);
        }//endif

        //SIGNAL THREAD TO WRITE TO FILE
        readyToWrite.Release();
    }//method


    public static void fillFile(String filename, String value) {
        try {
            StreamWriter w = File.AppendText(filename);
            using (w) {
                w.Write(value);
                w.Flush();
            }//using
        } catch (Exception e) {
            throw new Exception("Can not write to file", e);
        }//try
    }//method


}//class

Thanks!

  • 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-16T08:12:14+00:00Added an answer on May 16, 2026 at 8:12 am

    You are initializing the Semaphore wrong. Try : new Semaphore(0, 1);

    See this MSDN page.

    And a WaitEvent is probably easier and more appropriate for this task.

    The 2 code changes would be:

    //Semaphore readyToWrite=new Semaphore(1, 1);
    var readyToWrite = new ManualResetEvent(false);
    
    //readyToWrite.Release();
    readyToWrite.Set();
    

    Also, setting the Priority is usually a bad idea. You’re not going to gain anything here (it’s an I/O thread), so best leave it.

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

Sidebar

Related Questions

I would like to read/write the window information file (extended file properties) using c#
I am trying to write some messages to a text file using Debug.WriteLine(Message). Here
I am using C# and Visual Studio 2010. When I use OutputDebugString to write debug information,
I've inherited a C# Winform application that write database information into various file formats
I am trying to use the Event Log to write some debug information and
Is there a cleaner way for me to write debug level log statements? In
Lets write simple console application (debug mode): static void Main(string[] args) { Process p
I write the following macro for debug convinience, 1 #ifndef DEF_H 2 #define DEF_H
I'm trying to write a macro that toggles between release/debug solution configurations in Visual
Suppose you have a file that you are programmatically logging information into with regards

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.