Hi,
I have an issue below I'd love help with, involving a static variable,
Application_Start, 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_Start event handler, I set this:
serverName =
HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
So far, so good.
Further along on Application_Start, a new thread is created, using
something like:
thread = new System.Threading.Thread(
new System.Threading.ThreadStart(MyFunction));
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_Start to fire again. This
makes sense to me.
But if that happened, would my "housekeeping" 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_Start?
Thanks in advance for any help with this.