Threading, using APPLICATION objects

J

John Cosmas

I need to execute some threads that load items into my APPLICATION object. I
haven't figured out how to do that when I fire off a thread on a page, that
takes its time and loads data into the APPLICATION level object which will
be used later. Here is an example code I've used to fire it off.

Dim pclsUserServices As clsUserServices = New clsUserServices
Dim pobjUserServices As Thread
pobjThread100 = New Thread(New ThreadStart(AddressOf
pclsUserServices.Initialize))
pobjUserServices.Start()
pobjThread100.Start()

What I need to do int he Initialize example here, it so actually load some
data into the APPLICATION object. When I try to do this in the CLASS, I
noticed that the APPLICATION object is not supported. Any ideas on how to
accomplish this?
 
B

Brock Allen

Can you explain a bit more what you're trying to do. It sounds like you want
to load some data in a background thread so it's available later. Where do
you want to save this loaded data? You said "Application", but I'm not sure
if you mean the HttpApplication-derived objects or the old Application state
bag held over from classic ASP. If you're talking about the later, then you
can pass the Context.Application over to the secondary thread (just set it
as a field on your clsUserServices). I think you're going to have a slew
of sychronization problems tho. If I were doing this, I'd just do a lazy
load on the data you need form the database. It'll make for less error prone
code, IMO.
 
B

Bruce Barker

as you new thread is not associated with a request context, you should pass
the application object to the new thread. you shoudl also use a mutex to
control creating the thread or you can have mulitple threads running at
once.

-- bruce (sqlwork.com)
 
J

John Cosmas

Brock;

Thanks for your response. Yes, you're heading in the right direction, but I
need confirmation on how to pull this off, since I'm a little new to all of
this. I'm trying to load some data that the customer will use later in the
application. Since, this information is READ ONLY, this THREAD is intended
to be FIRE and FORGET. So, based on your response, I am to assume that I
need to pass the APPLICATION("myData") into the clsUserServices.SomeMethod.
If so, I am also assuming that you will then take the CONTEXT.APPLICATION
and do what you need to, and I can read from it later in my pages without a
problem? I hope this clarifies it a bit.
 
B

Brock Allen

Well, my reason for suggesting doing it in a lazy-load manner, rather than
the 2nd thread is there is the window of time where the data has not yet
been loaded and a request might arrive that wants that data. Now you have
to do locking. So, IMO, the locking is more effort and error prone (especially
if you've not done this before) than just writing it to be lazy loaded. Now,
of course, if the data takes seconds to load, then that's different and the
approach you're going for is proabbaly worth that effort.

So off the top of my head here's some pseudo code that should give you the
idea:

Application_Start()
{
WorkerClass c = new WorkerClass();
Application["WorkerClass"] = c;
Thread t = new Thread(c.Init);
t.Start();
}

WorkerClass.cs:

class WorkerClass
{
Data _data;
Mutex Lock = new Mutex();

void Init()
{
Lock.WaitOne();
if (_data == null) _data = GetDataFromDB();
Lock.ReleaseMutex();
}

Data GetData()
{
if (_data != null) return _data;
Lock.WaitOne();
if (_data == null) Init();
Lock.ReleaseMutex();
return _data;
}
}

SomePage.aspx:

void Page_Load()
{
WorkerClass c = Application["WorkerClass"];
Data d = c.GetData();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top