Solved

Using Application Var's in a background thread

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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…
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.

708 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

17 Experts available now in Live!

Get 1:1 Help Now