473,545 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static variable in Global.asax, Application_Sta rt 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.

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

Nov 19 '05 #2
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
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_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.


Nov 19 '05 #3
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;
Nov 19 '05 #4
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;
Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3910
by: Bob Johnson | last post by:
Hello people. Short background -- I am constructing a web site that will have several display tabs that are hooked up to different datatables in a SQL server. Some of these tables are VERY large -- several million+ records. The tables have records that are timestamped over several years and months/quarter/weeks... depending on the...
1
4082
by: localhost | last post by:
In the Application_OnStart, I make some data calls and place the results in the HTTP cache. Sometimes, the database is not available when the application starts up, so I want to sleep for 20 seconds and then restart the entire application. What is the best way to do that? Thanks.
3
6116
by: hansiman | last post by:
I use Application_Start in global.asax to set some physical folder paths ie.: Application("pdf") = "c:\www\<site>\pdf\" global.asax uses code behind. When I move the project from the dev to the production server the global.asax code behind is compiled into the project.dll (i guess). This renders the physical paths unusable...
8
4849
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical differences in the way both the objects are handled by IIS. Are both objects stored in different memory spaces? I can access both the objects in my web...
4
2130
by: NoNickname | last post by:
Hi, I need to get a string from a COM component at application start. (It's a Long Story and I cannot change this fact.) In ASP.NET 1.1, I simply called this COM component in Global.asax.cs from Application_Start, stuck the string in the HttpApplication.Application object and life was good. However, I've noticed in ASP.NET 2.0 you are...
14
1430
by: Steven Blair | last post by:
Using ASP.NET 2.0 and Visual Studio 2005 I have website and a DLL attached to it (BusLayer). The class inside the dll (also imaginatively named BusLayer) havea number of static variables. One example: public static connectionString; Website is running from IIS.
6
2370
by: depalau | last post by:
I'm running into some issues on maintaining a static variable across the lifetime of a web service and I was wondering if anyone could help. Background: We have developed a C#/1.1 web service running on IIS 5/6 that interfaces with a 3rd party DLL referenced by using the DLLImport attribute. Interacting with this 3rd party software...
13
1700
by: michel | last post by:
Hi, i created a class 'test' with a method 'descrlimit()' (no matter). That method is used in a lot of pages in the application, so i need to put this code a lot of time: "Dim odescr As New test" In order to avoid that, i wonder whether it would be possible to put that line in a central place, like global.asax. I tried this:
4
4509
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or...
0
7453
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7742
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5318
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1865
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1007
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.