Solved

Using Application Var's in a background thread

Posted on 2011-03-04
2
306 Views
Last Modified: 2013-11-19
Hi there,
This is what I tried to do:
In global.asax:
// read-only in the client sessions, write only in a background thread
Application["OnSaleCounter"] = (Int64)1;
// SET UP BACKGROUND THREAD
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(CheckOpenManager.OpenForBidingThread));
th.IsBackground = true;  th.Start();
In the CheckOpenManager class I try to read and update this Counter,
System.Web.HttpContext.Current.Application["OnSaleCounter"]
but I get the Error = null
because the code is not running in httpContext, is there another way to
read/write application variabels in a background thread or do I put it in a seperate
dummy page? And can I call that page in global.asax?

Jürgen
0
Comment
Question by:jurgen_thys
2 Comments
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35038992
I'm not sure if that's considered safe or not to be using threads at all in a web application. Could be, I just don't know - seems dangerous though. ;)

If you do continue to use threads make sure there's some thread synchronization to make sure more than one thread isn't trying to use the same resource at the same time.  Now that I think about it - I'm not sure you have any way to make access to the ApplicationContext thread-safe.

But to answer your question, you can try passing a reference to the HttpContext in the thread's start.

Thread th = new Thread(OpenForBidingThread);
th.Start(HttpContext.Current);

private void OpenForBidingThread(object threadArg)
{
    HttpContext context = threadArg as HttpContext;
}

Open in new window

0
 

Author Closing Comment

by:jurgen_thys
ID: 35046646
Hi,
This solution works PERFECT. I can finally update a Application variabel
in a background stream.

Thanks very much!

Jürgen,
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question