Solved

Using Application Var's in a background thread

Posted on 2011-03-04
2
305 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

810 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