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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:55:04+00:00 2026-05-12T15:55:04+00:00

I have a static class that creates several worker threads in its constructor. If

  • 0

I have a static class that creates several worker threads in its constructor. If an exception occurs before the workers have been created my Application.ThreadException handler (used to shut the app down if an error not known to be recoverable occurs) triggers normally and everything is fine. Once the first worker thread has been created however in addition to the handler firing I get an “MYAPP has encountered a problem and needs to close. We are sorry for the inconvenience.” dialog for MS error reporting.

In this specific instance I can reorder the code to create the threads last (after any resource initialization/access problems that could’ve triggered an exception) but that’s no more than a bandaid to the problem and doesn’t give me any information about what’s actually going on.

Hopefully I’ve excised enough code from my app to show what I’m trying to do here.

class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        try
        {
            Application.Run(theForm);
            theForm.Dispose();

        }
        catch (Exception e)
        {
            //doing this to use the same handler here and for Application.ThreadException
            ThreadExceptionEventArgs argsEx = new ThreadExceptionEventArgs(e);
            FatalExceptionHandler(null, argsEx);

        }
        finally
        {
            MyStaticClass.KillThreads();
        }
    }

    public static void FatalExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs ex)
    {
        Exception e = ex.Exception;
        try
        {
            //lots of stuff to give more useful error messages for known problems
            //and display them in a messagebox.
        }
        // if anything went wrong scraping the exception text for formatting, show the raw value.
        catch
        {
            MessageBox.Show(e.Message);
            return;
        }
        // after showing the MessageBox, close out the app.
        finally
        {
            System.Environment.Exit(1);
        }
    }
}

class MyStaticClass
{
    static MyStaticClass()
    {
        myThread = new Thread(new ThreadStart(SomeMethod));

        //if this exception is thrown everything works normally
        //Throw new Exception("KABOOM");

        myThread.Start();

        //if this exception is thrown a windows error reporting dialog appears 
        //along with the messagebox from program.FatalExcetion handlder             
        //Throw new Exception("KABOOM");
    }


    public void KillThreads()
    {
        //clean up the worker threads
    }
}
  • 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-12T15:55:04+00:00Added an answer on May 12, 2026 at 3:55 pm

    Static constructors are not called at a specific time during application startup. In particular, if you never reference MyStaticClass from another class, it may never be initialized. Since you can’t reason about this in any sane way, you should provide a StaticInitialize() method (or similar) for the types with non-trivial code in the static constructor plus cases where the static initialization code really needs to run. Move the code from the static constructor to the static initialization method.

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • 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 Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

I've got a pretty strong background in C-style languages. And have worked on several
I'm trying to show someone a use for interfaces in a crazy situation they've
I frequently run into the problem that I have to preserve state between several
I may be going about this backwards... I have a class which is like

Trending Tags

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

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.