469,572 Members | 1,629 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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_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.

Nov 19 '05 #1
4 6100
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:
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.

Nov 19 '05 #2
My suspicion would be that you have an exception being thrown in
Application_Start 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
http://www.OdeToCode.com/blogs/scott/

On 22 Jul 2005 11:18:47 -0700, "Marc Missire" <mm******@gmail.com>
wrote:
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.


Nov 19 '05 #3
Thanks to you both for the comments.
The code in Application_Start looks like this, however:

serverName =
HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
applicationPath = HttpContext.Current.Request.ApplicationPath;
Nov 19 '05 #4
Thanks to you both for the comments.
The code in Application_Start looks like this, however:

serverName =
HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
applicationPath = HttpContext.Current.Request.ApplicationPath;
Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by localhost | last post: by
3 posts views Thread by hansiman | last post: by
8 posts views Thread by Vishwanathan Raman | last post: by
4 posts views Thread by NoNickname | last post: by
14 posts views Thread by Steven Blair | last post: by
6 posts views Thread by depalau | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.