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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:39:52+00:00 2026-06-14T21:39:52+00:00

In the application there are 2 pages, CompletePoll.aspx, Default.aspx. CompletePoll.aspx –> Page_Load() Ultoo u

  • 0

In the application there are 2 pages, CompletePoll.aspx, Default.aspx.

CompletePoll.aspx –> Page_Load()

          Ultoo u = new Ultoo();
          u.UserName = Request["username"].ToString();
          u.Password = Request["password"].ToString();
          new Thread(u.CompletePoll).Start();

CompletePoll()

          .......
          .......
          String str = "Question:" + QuestionGenerator.GetNextQuestion(); /*Here i am getting Type initializer exception*/
          .......
          .......

QuestionGenerator

          public static class QuestionGenerator
          {
               private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
               private static string[] SecondParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart2.txt")).ReadToEnd(). Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
               private static Random r = new Random();

               public static string GetNextQuestion()
               {
                    return FirstParts[r.Next(0, FirstParts.Length - 1)] + " " + SecondParts[r.Next(0, SecondParts.Length - 1)] + "?";
               }
          }

But if i am calling Default.aspx first and then CompletePoll.aspx the code is working fine.

Default.aspx –> Page_Load()

         Label1.Text = QuestionGenerator.GetNextQuestion();

So here my problem is if i am accessing CompletePoll.aspx first i am getting TypeInitializer Exception. If i am accessing Default.aspx first and then CompletePoll.aspx, I am not getting any problem. Whats wrong in my code, am i missing something? How can i access CompletePoll.aspx first?

  • 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-14T21:39:53+00:00Added an answer on June 14, 2026 at 9:39 pm
    private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
    

    That isn’t right. This checks HttpContext.Current once, when the type is initialised, saves the result, and never attempts to read it again. The never checking again can be correct when the first time succeeds, but the first time will require HttpContext.Current to not be null. If the first attempt causes an exception, it won’t be reinitialised later. You cannot be sure when exactly the class gets initialised, so you cannot be sure whether HttpContext.Current is set at that point (and it won’t be if you call it from a thread).

    Also, this does not call StreamReader.Dispose, so it will leave the reader and file itself open until the garbage collector happens to run.

    A safer way would be something like

    private static string[] firstParts; // field
    
    private static string[] FirstParts // property
    {
        get
        {
            if (firstParts == null)
            {
                using (var reader = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")))
                    firstParts = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            }
            return firstParts;
        }
    }
    

    This will make sure reader.Dispose() gets called, will make sure the file is read when the property is first accessed instead of when the type is initialised, will make sure any exceptions actually tell you what’s going on in a more straightforward way, and will make sure the rest of the type is usable even if FirstParts cannot get set.

    However, it still requires that you do not read FirstParts from a thread. You can avoid that problem by reading it once before starting the thread:

    Ultoo u = new Ultoo();
    u.UserName = Request["username"].ToString();
    u.Password = Request["password"].ToString();
    QuestionGenerator.Initialize(); // from the main thread
    new Thread(u.CompletePoll).Start();
    
    
    public static class QuestionGenerator
    {
        public static void Initialize()
        {
            var firstParts = FirstParts;
            var secondParts = SecondParts;
            // merely reading the properties is enough to initialise them, so ignore the results
        }
    }
    

    Once the thread has started, after Initialize() has been called, you can access FirstParts and SecondParts reliably.

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

Sidebar

Related Questions

I have a asp.net mvc application. Some pages require SSL, is there a way
In a web application all pages are loading using ajax. Some pages have there
In my asp.net application I have two pages like new students and edit students.
In the base template for the pages of my Wicket application, there's a form
I've developed a simple struts2 web application. In that there are two pages index.jsp
I have managed to localize the view pages in my application but there are
I have a J2EE based web application. In one of the pages there is
There are two pages in my silverlight project: MainPage-default page and SecondViewPage an added
Is there an application that could benchmark or check what pages makes my application
There are two pages in my jQuery mobile application. When a list item on

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.