Timer for Data Fetch?

L

lucius

With ASP.NET/Framework 1.1, I would like to have a class that
instances on application startup, and every 60 seconds after that does
some work (actually a database fetch). Then any .aspx/.asmx page could
go to that global instance instead of fetching the data themselves.

Can anyone show an example of how to best do this?

A 2.0 example would be great too, but my main issue is making this
happen on 1.1.



Thanks.
 
M

Mark Rae

With ASP.NET/Framework 1.1, I would like to have a class that
instances on application startup, and every 60 seconds after that does
some work (actually a database fetch). Then any .aspx/.asmx page could
go to that global instance instead of fetching the data themselves.

ASP.NET really isn't suited for this sort of task. For one thing, an ASP.NET
application does not run continuously - it will stop when the last remaining
session expires and won't start up again until another session is created...
Can anyone show an example of how to best do this?

Well, I suppose you could have a page which refreshes itself every sixty
seconds: http://www.html-reference.com/META_httpequiv_refresh.htm, queries
the database and persists DataSets in the Application object, but I *really*
don't advise doing this...


What are you hoping to achieve anyway...?
 
S

Steven Cheng[MSFT]

Hi Mark,

For your scenario, what you want to do is like a background task scheduler
or timer which constantly do jobs in background and other pages can query
data from its status storage. I think you can achieve this through the
following means:

** define a public class which expose some static property and methods so
that other page can access it during the ASP.NET application's entire
lifecycle.

** This class will create a background thread(you can also create a timer
instance for this) and do some work in the thread procedure function.

Here is a very simple demo class:

======================
public class GlobalDataStore
{
public static string CurrentMessage;

private static Thread _thread = null;
private static bool _run = false;

public GlobalDataStore()
{

}


public static void StartProcessor()
{
if (_thread != null)
{
return;
}

_thread = new Thread(new ThreadStart(Thread_Proc));

_run = true;
_thread.Start();


}

public static void StopProcessor()
{
if (_thread != null)
{
_run = false;
_thread.Join();
}
}

private static void Thread_Proc()
{
while (_run)
{
CurrentMessage = "Ticks: " + DateTime.Now.Ticks;

Thread.Sleep(2000);
}
}
}
=======================

thus, you can call the StartProcessor method in Global.asax object
"Application_Start" event or in any other place you want to start the
background task. Then, you can also query the status or result producted by
the background thread through some exposed public static properties of this
class(like the "CurrentMessage" here).

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Thanks Mark :)

My mistake, I should reply to lucius.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Lucius,

Have you any further question on this or does the suggest in my last reply
help some? Please feel free to post if there is anything else we can help.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top