System.Timers.Timer in Global.asax

M

Mark Rae

Hi,

I've seen several articles about using System Timers in ASP.NET solutions,
specifically setting them up in Global.asax' Application_OnStart event.

I'm thinking about the scenario where I might need to carry out some
back-end processing without pausing the individual users' Session while that
process runs.

E.g. I might provide the ability for a user to upload a text file of job
numbers, the purpose of which is to flag the job as completed in a SQL
Server database, and send an email to the owner of the job telling them that
their job is completed. It might take half a second or so to process each
job, but the user is allowed to upload a text file containing a maximum of
100 records, so this would make them wait around 50 seconds while the file
is processed. Unacceptable.

Ordinarily, this would be a perfect candidate for MSMQ or a Windows service.
However, my ISP does not support MSMQ or allow me to deploy Windows
services, so the only options open to me (AFAIK) are the .NET Framework v1.1
and SQL Server 2000, though I do have access to my site's aspnet_client
directory.

Therefore, I wondered if it might be possible to instantiate a System.Timer
in the Application_OnStart event which would make a call to a static
function in a Class module which "sniffed" for these uploaded files and
processed them. This would be the same amount of work for ASP.NET to do, but
the user would not have to wait while their file was processed. Instead, I'd
display a message telling them that their file has been successfully
uploaded and "will be processed in the next few minutes" or something.

Looking for some advice. Is this a good idea, not a good idea, a really
stupid idea...?

Any other suggestions for achieving this within the confines of what my ISP
offers?

Any assistance gratefully received.

Mark Rae
 
Y

Yunus Emre ALPÖZEN

You can create a new thread from your ASP.NET application and store the
thread object in Session which might be used to stop execution of the
thread. I don't advise you to use timers in an ASP.NET. You can easily
simulate timers using threads on storing thread in Application object.. I
did it, and it works fine...
 
M

Mark Rae

You can create a new thread from your ASP.NET application and store the
thread object in Session which might be used to stop execution of the
thread. I don't advise you to use timers in an ASP.NET. You can easily
simulate timers using threads on storing thread in Application object.. I
did it, and it works fine...

Thanks for the advice - do you have any sample code?
 
Y

Yunus Emre ALPÖZEN

//...
Thread th = new Thread(new ThreadStart(f));
th.IsBackground=true;
Session["Thread"]=th;
th.Start();
//...
public void f()
{
try
{
// Do operations requires too many times...
}
finally
{
Session.Remove("Thread");
}
}

On the page put a button to kill the process... Sample code should be as
follows;
///....
Thread th = Session["Thread"] as Thread;
if (th!=)
th.Abort();
Session.Remove("Thread");

If you implement a class for importing and parsing file. You can create an
instance in the f function implementation. And store that object in session.
And access its additional information via this object. Session will be valid
for both thread.
Hope this helps. Any additional question ?
 
S

Scott Allen

Hi Yunus:
I don't advise you to use timers in an ASP.NET.

Why not?
You can easily
simulate timers using threads on storing thread in Application object.. I
did it, and it works fine...

The 2 areas of concern that jump to mind when I look at the code in a
later post are:

1) scalability (I wouldn't want 500 users to each be using a dedicated
thread)

2) Abort doesn't work well if the thread needs to guarantee certain
results - for instance - if the thread has to guarantee it won't leave
an open file or database transaction.
 
W

William F. Robertson, Jr.

I would recommend QueueUserWorkItem. There is really not point in having a
single dedicated thread to process these files. Using a single thread, you
will not be able to process two files at the same time.

QueueUserWorkItem takes a WaitCallback and a optional object.

QueueUserWorkItem( WaitCallback );
QueueUserWorkItem( WaitCallback, object );

The WaitCallback is a method that will be called by the framework using a
thread from the thread pool. It will be passed the object queued, or null
if not used. The thread pool is a great resource for running little tasks
like this.

I am not sure what you skill level is, but the code snippet MS provides on
this page is a decent starter for this.

http://msdn.microsoft.com/library/d...dingthreadpoolclassqueueuserworkitemtopic.asp

You could call this method when the user needs to process the file, button
click? QueueUserWorkItem will return immediately so there will be no delay
from the user's perspective.

HTH,

bill
 
Y

Yunus Emre ALPÖZEN

I used timers in a web application. But it caused some trouble on my
application. I used timer for a different issue. I would like to execute a
code snippet at every night 03:00 am. I add a timer to my project and
control the time at every minute. Then i noticed that timer didn't call my
code snippet. I tried to debug my code in some way. But I couldn't. I wrote
a Process manager using threads. And everything works fine. I am not sure.
But I thought that the reason of the problem is what will happened if there
is no request at that time. A few minutes ago, I googled it and find out the
exact solution. I am just pasting it:

"System.Threading.Timer is a simple, lightweight timer that uses callback
methods and is served by threadpool threads. You might also consider
System.Windows.Forms.Timer for use with Windows forms, and
System.Timers.Timer for server-based timer functionality. These timers use
events and have additional features."

The right answer is <just use "System.Threading.Timer">
 
M

Mark Rae


I was about to ask the same thing!
1) scalability (I wouldn't want 500 users to each be using a dedicated
thread)

Indeed not.
2) Abort doesn't work well if the thread needs to guarantee certain
results - for instance - if the thread has to guarantee it won't leave
an open file or database transaction.

And what happens, say, if the user kicks off a process in a thread which
might take 60 seconds to run, but the user logs out immediately by clicking
a "Log Out" button which tears down their session. Does the thread continue
to run?

So, is there any *actual* issue with instantiating a System.Threading.Timer
object in the Application_OnStart event of Global.asax?
 
S

Scott Allen

As long as you chose the correct type of timer (as Yunus pointed out,
the Timer in the System.Threading namespace is well suited for this
work).

There are still some gotchas, though. The timer activity is not enough
to keep an application "alive" on platforms like win2003 which can
spin down an application after so many minutes of inactivity. You
still have to have incoming requests to keep the timer alive [or have
the timer callback make requests to the application :)].
 
Joined
May 31, 2009
Messages
1
Reaction score
0
pls refer to the technique outlined here :

blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top