473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

System.Timers.Timer in Global.asax

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
Nov 19 '05 #1
9 7833
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,
Yunus Emre ALPÖZEN

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2*****************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #2
"Yunus Emre ALPÖZEN" <ye***@msakademik.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?
Nov 19 '05 #3
//...
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 ?
--
Thanks,
Yunus Emre ALPÖZEN

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:up**************@TK2MSFTNGP15.phx.gbl...
"Yunus Emre ALPÖZEN" <ye***@msakademik.net> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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?

Nov 19 '05 #4
"Yunus Emre ALPÖZEN" <ye***@msakademik.net> wrote in message
news:ue**************@TK2MSFTNGP14.phx.gbl...
Hope this helps. Any additional question ?


Thanks very much - I'll try it this evening...
Nov 19 '05 #5
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/de...kitemtopic.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

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2*****************@TK2MSFTNGP15.phx.gbl...
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

Nov 19 '05 #6
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 3 Feb 2005 12:32:25 +0200, "Yunus Emre ALPÖZEN"
<ye***@msakademik.net> wrote:
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...


Nov 19 '05 #7
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">

--
Thanks,
Yunus Emre ALPÖZEN

"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:fc********************************@4ax.com...
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.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 3 Feb 2005 12:32:25 +0200, "Yunus Emre ALPÖZEN"
<ye***@msakademik.net> wrote:
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...

Nov 19 '05 #8
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:fc********************************@4ax.com...
I don't advise you to use timers in an ASP.NET.
Why not?


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?
Nov 19 '05 #9
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 :)].

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Thu, 3 Feb 2005 18:20:11 -0000, "Mark Rae"
<ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Scott Allen" <sc***@nospam.odetocode.com> wrote in message
news:fc********************************@4ax.com.. .
I don't advise you to use timers in an ASP.NET.


Why not?


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?


Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antoine | last post by:
Hello, Does anybody know a way to retreive the running timers (their ids) in a html page? I cannot find something in the html dom at first sight. Is there collection of timers available (like...
3
by: Peter Johnsson | last post by:
How come the eventhandler for the timer's elapsed time event is called over and over again, even though the AutoReset property is set to false, if you assign a new value to the timer objects...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
2
by: rgparkins | last post by:
Hi Guys Maybe a simple and easy solution to this, but I keep getting exception object not set to instance etc etc. I have a Timer that is initiated in Application_Start in Global.asax. This...
2
by: jajalc | last post by:
Hi all, WE have a windows service using .net and which uses a System.Timers.Timer..all the of code works fine..... but after a extended period of time the timer just stops firing the elasped...
2
by: Rob | last post by:
I've got the following skeleton in my HttpApplication (global.aspx) file: Public Sub New() MyBase.New() ApplicationTimer = New System.Timers.Timer ApplicationTimer.BeginInit()...
2
by: Mubi | last post by:
I am using VS2003 and developing web services. We also need a timed activity to be performed on the server. If i drag a timer and place it on global.asax, add interval and timer handler. it doesnot...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.