Solved

Using Application Var's in a background thread

Posted on 2011-03-04
2
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
original content 7 121
"The resource cannot be found" 1 41
Run tasks synchronously in background thread 19 80
Read top line from CSV file 14 57
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.
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…
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…

739 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