Solved

Using Application Var's in a background thread

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video teaches users how to migrate an existing Wordpress website to a new domain.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now