static variable in Global.asax, Application_Start assignment of it, and a background thread.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marc Missire

    static variable in Global.asax, Application_Start assignment of it, and a background thread.

    Hi,

    I have an issue below I'd love help with, involving a static variable,
    Application_Sta rt, and a background thread.

    In global.asax.cs I have a static variable (outside any method) with a
    default value, such as:

    private static string serverName = string.Empty;


    In the Application_Sta rt event handler, I set this:

    serverName =
    HttpContext.Cur rent.Request.Se rverVariables["SERVER_NAM E"];


    So far, so good.
    Further along on Application_Sta rt, a new thread is created, using
    something like:

    thread = new System.Threadin g.Thread(
    new System.Threadin g.ThreadStart(M yFunction));
    thread.Start();

    This thread hangs around, periodically doing some needed housekeeping.
    Among other things, it uses the "serverName " variable in a read-only
    fashion.


    This seems to work. However, it appears that "serverName " is empty once
    in a great while, when accessed in the thread created above.

    My question:

    I've heard that an application recycle event could cause the static
    variables to be reloaded and the Application_Sta rt to fire again. This
    makes sense to me.

    But if that happened, would my "housekeepi ng" thread be in trouble?
    Since it uses "serverName ", could it be accessing this right as it is
    reloaded due to either its static initializer, or the assignment in
    Application_Sta rt?



    Thanks in advance for any help with this.

  • Brad Roberts

    #2
    RE: static variable in Global.asax, Application_Sta rt assignment of it

    It has been a while since I even thought of threads....but I think you may be
    in trouble with the lost thread. Could you gracefully kill the thread in
    Application_End to avoid any "leakage" problems. That would make sense to me.
    (I am not an expert on threading...)

    "Marc Missire" wrote:
    [color=blue]
    > Hi,
    >
    > I have an issue below I'd love help with, involving a static variable,
    > Application_Sta rt, and a background thread.
    >
    > In global.asax.cs I have a static variable (outside any method) with a
    > default value, such as:
    >
    > private static string serverName = string.Empty;
    >
    >
    > In the Application_Sta rt event handler, I set this:
    >
    > serverName =
    > HttpContext.Cur rent.Request.Se rverVariables["SERVER_NAM E"];
    >
    >
    > So far, so good.
    > Further along on Application_Sta rt, a new thread is created, using
    > something like:
    >
    > thread = new System.Threadin g.Thread(
    > new System.Threadin g.ThreadStart(M yFunction));
    > thread.Start();
    >
    > This thread hangs around, periodically doing some needed housekeeping.
    > Among other things, it uses the "serverName " variable in a read-only
    > fashion.
    >
    >
    > This seems to work. However, it appears that "serverName " is empty once
    > in a great while, when accessed in the thread created above.
    >
    > My question:
    >
    > I've heard that an application recycle event could cause the static
    > variables to be reloaded and the Application_Sta rt to fire again. This
    > makes sense to me.
    >
    > But if that happened, would my "housekeepi ng" thread be in trouble?
    > Since it uses "serverName ", could it be accessing this right as it is
    > reloaded due to either its static initializer, or the assignment in
    > Application_Sta rt?
    >
    >
    >
    > Thanks in advance for any help with this.
    >
    >[/color]

    Comment

    • Scott Allen

      #3
      Re: static variable in Global.asax, Application_Sta rt assignment of it, and a background thread.

      My suspicion would be that you have an exception being thrown in
      Application_Sta rt and the serverName reference is never set. Try
      setting a breakpoint in the method and stepping through with the
      debugger a few times, or add a try catch and set serverName to a value
      that will let you know an exception happened.

      I'm pretty sure you'll discover the problem has nothing to do with
      resets.

      --
      Scott


      On 22 Jul 2005 11:18:47 -0700, "Marc Missire" <mmissire@gmail .com>
      wrote:
      [color=blue]
      >Hi,
      >
      >I have an issue below I'd love help with, involving a static variable,
      >Application_St art, and a background thread.
      >
      >In global.asax.cs I have a static variable (outside any method) with a
      >default value, such as:
      >
      >private static string serverName = string.Empty;
      >
      >
      >In the Application_Sta rt event handler, I set this:
      >
      >serverName =
      >HttpContext.Cu rrent.Request.S erverVariables["SERVER_NAM E"];
      >
      >
      >So far, so good.
      >Further along on Application_Sta rt, a new thread is created, using
      >something like:
      >
      >thread = new System.Threadin g.Thread(
      > new System.Threadin g.ThreadStart(M yFunction));
      >thread.Start() ;
      >
      >This thread hangs around, periodically doing some needed housekeeping.
      >Among other things, it uses the "serverName " variable in a read-only
      >fashion.
      >
      >
      >This seems to work. However, it appears that "serverName " is empty once
      >in a great while, when accessed in the thread created above.
      >
      >My question:
      >
      >I've heard that an application recycle event could cause the static
      >variables to be reloaded and the Application_Sta rt to fire again. This
      >makes sense to me.
      >
      >But if that happened, would my "housekeepi ng" thread be in trouble?
      >Since it uses "serverName ", could it be accessing this right as it is
      >reloaded due to either its static initializer, or the assignment in
      >Application_St art?
      >
      >
      >
      >Thanks in advance for any help with this.[/color]

      Comment

      • Marc Missire

        #4
        Re: static variable in Global.asax, Application_Sta rt assignment of it, and a background thread.

        Thanks to you both for the comments.
        The code in Application_Sta rt looks like this, however:

        serverName =
        HttpContext.Cur rent.Request.Se rverVariables["SERVER_NAM E"];
        applicationPath = HttpContext.Cur rent.Request.Ap plicationPath;

        Comment

        • Marc Missire

          #5
          Re: static variable in Global.asax, Application_Sta rt assignment of it, and a background thread.

          Thanks to you both for the comments.
          The code in Application_Sta rt looks like this, however:

          serverName =
          HttpContext.Cur rent.Request.Se rverVariables["SERVER_NAM E"];
          applicationPath = HttpContext.Cur rent.Request.Ap plicationPath;

          Comment

          Working...