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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:40:38+00:00 2026-05-28T05:40:38+00:00

I have a program which write logging as Text File. namespace logging { using

  • 0

I have a program which write logging as Text File.

namespace logging
{
    using System;
    using System.IO;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Text;

    public class log
    {
        private static string lpath;

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        public static void Info(string user, string info, string txt)
        {
            StreamWriter writer;
            string str = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();

            .....

            if (!File.Exists(path))
            {
                writer = new StreamWriter(path, true);
                writer.WriteLine(str3 + " " + info + " => " + txt);
                writer.Flush();
                writer.Close();
            }
            else
            {
                writer = File.AppendText(path);
                writer.Write(str3 + " " + info + " => " + txt + "\n");
                writer.Flush();
                writer.Close();
            }
        }
    }
}

Then I have a function.

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    Thread thread_BP_Print = new Thread(BoardingPass_Print);          
    thread_BP_Print.Start();

    // Simultaneously, do something on the main thread.
    BaggageTag_Print();
    bgw.RunWorkerAsync();
}

Two of the inner functions (BoardingPass_Print() and BaggageTag_Print()) call the same logging function.

logging.log.Info(username, "Query Trace > ", sql);

Because of my invoking thread methods, I think I face the error:

The process cannot access the file ‘C:\Folder\2012-01-17-Name.txt’ because it is being used by another process.

Could anyone give me better solution to interact with thread and logging to text file without facing error message like this?

Every suggestion will be really appreciated.

  • 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-28T05:40:39+00:00Added an answer on May 28, 2026 at 5:40 am

    You would have to make the writing thread-safe (with lock). Your current code also is not exception-safe.
    You can solve both issues by using the right library method.

    You cannot (easily) assure safe access to a file from multiple processes, but as long as its only written by 1 instance of the application you can sync the threads.

    private static object locker = new object();
    public static void Info(string user, string info, string txt)
    {        
        string str = Assembly.GetExecutingAssembly().GetName().CodeBase.ToString();
        string str3 = ...
        lock (locker)
        {
           File.AppendAllText(path, str3 + " " + info + " => " + txt + "\n");
        }
    }
    

    AppendAllText is a static method, it will handle the new/existing file issue and the resource management correctly.

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

Sidebar

Related Questions

I have created a small program which logs text data to a file on
I have a program which reads all the file system file/dir names, size etc.
I have created a program which takes a text file full of 3 letter
How to write a java program which will tell me whether I have internet
I have a program which doesn't once reference System.I0, coded in vb.net, yet for
i have question how write program which calculates following procedures http://en.wikipedia.org/wiki/Tetration i have exponential
I have a program which writes its output using ofstream. Everything works perfectly fine
suppose we have two numbers i want write program which print common bits subsequent
I have a python program which takes input from stdin. Now, I've to write
I have a program which would use the Application Role to write data to

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.