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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:17:26+00:00 2026-05-16T07:17:26+00:00

I’m using Visual Studio 2010 to write a simple C#/.NET GUI app, wherein I

  • 0

I’m using Visual Studio 2010 to write a simple C#/.NET GUI app, wherein I use a Logger class to write tracing/debugging info to a single file from within all of the various classes of the project. (See source code below.)

Each class’s constructor writes an entry to the log when one of its object types is instantiated. One of those classes is a custom GUI controller component (class FileAttributesCtl), which is contained in a couple of the GUI forms used by the program.

The problem I’m having is that two logfiles are created, about 200 msec apart. The first logfile contains (only) a message that a FileAttributesCtl object has been constructed, and the second contains all of the other messages written to the (supposedly) shared logfile output stream. So every time I execute my project code, I get two logfiles.

Stranger still, every time I rebuild my project (F6), a logfile is created for the FileAttributesCtl object, indicating that an control object of this type is actually instantiated during the build process.

This apparently has something to do with threading. If the logfile is not named uniquely (i.e., if I do not append a unique date/time string to the filename), I get an exception, indicating that more than one process (which is actually the VS2010 process itself) is currently using the file.

So my question is: How do I get the singleton object to actually be a single object?

A secondary question is: Why is VS2010 acting this way?

//----------------------------------------
// Logger.cs
class Logger
{
    // Singleton object
    private static Logger   s_logger =
        new Logger("C:/Temp/foo.log");

    public static Logger Log
    {
        get { return s_logger; }
    }

    private TextWriter  m_out;

    private Logger(string fname)
    {
        // Add a date/time suffix to the filename
        fname = ...;

        // Open/create the logging output file
        m_out = new StreamWriter(
            new FileStream(fname, FileMode.Create, FileAccess.Write,
                FileShare.Read));
        m_out.WriteLine(DateTime.Now.ToString(
            "'$ 'yyyy-MM-dd' 'HH:mm:ss.fff"));
    }

    ...
}

//----------------------------------------
// FileAttributesCtl.cs
public partial class FileAttributesCtl: UserControl
{
    private Logger  m_log = Logger.Log;

    public FileAttributesCtl()
    {
        m_log.WriteLine("FileAttributesCtl()");  //Written to first logfile
        InitializeComponent();
    }

    ...
}

//----------------------------------------
// FileCopyForm.cs
public partial class FileCopyForm: Form
{
    private Logger  m_log = Logger.Log;

    public FileCopyForm()
    {
        // Setup
        m_log.WriteLine("FileCopyForm()");       //Written to second logfile

        // Initialize the GUI form
        m_log.WriteLine("FileCopyGui.InitializeComponent()");
        InitializeComponent();
        ...
    }

    ...
}

Note: This is very similar to a question from Dec 2009:
Access to singleton object from another thread
but it does not have the answers to my question.

Update

Further investigation shows that the VS2010 is indeed instantiating the custom component during the build, probably so that it can render it in the Designer window.

Also, there are indeed two separate threads calling the Logger constructor (each having a different ManagedThreadID).

Using a static class initializer to construct the singleton object does not work; I still get two logfiles.

Resolution

Upon closer examination, I notice that the custom control is getting instantiated twice, and this is being shown in both logfiles.

Therefore I think the problem is entirely due to the fact that VS instantiates the custom control object prior to executing the program that results in the first logfile being created. The second logfile is then created after the program starts normal execution.

Thus the first logfile is simply a side effect of the build process, and does not really have anything to do with multiple threads executing during normal program operation.

The obvious solution is to remove all logfile side-effect code from the component constructors. Or simply just ignore the first logfile altogether.

  • 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-16T07:17:26+00:00Added an answer on May 16, 2026 at 7:17 am

    It could very well be that Visual Studio is building your UI component (to display in the designer) and in the process, your constructor is getting called which is why you’re seeing that log file during the build process.

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

Sidebar

Ask A Question

Stats

  • Questions 487k
  • Answers 487k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $victims does not appear to be within the scope of… May 16, 2026 at 8:22 am
  • Editorial Team
    Editorial Team added an answer NSURLConnection supports two modes of operation: asynchronous and synchronous. Neither… May 16, 2026 at 8:22 am
  • Editorial Team
    Editorial Team added an answer My advice: $ port uninstall ruby1.9 Then follow this: https://rvm.io/rvm/install/… May 16, 2026 at 8:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Related Questions

No related questions found

Top Members

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.