Anyway to Run Code Not Associated with Loading a Page?

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I'm building a site and, in addition to regular code associated with
ASP.NET, I have some tasks that are not related to loading a page.

For example, I need to write some code to poll sites for data, I also have
some code to perform some database processing. Both tasks will be lengthy,
perhaps an hour or so.

Does ASP.NET provide any means to run code other than the in the context of
loading a page?

(I realize I could write a C# desktop application, but that will require I
start from scratch. Also, a desktop application may or may not have access
to my online database.)

Thanks for any tips.

Jonathan
 
Not really but there is a trick to do so.

Idea is to use Application.Cache object and add there an object with
expiration time 1 hour (for example) and a callback function.
http://msdn.microsoft.com/en-us/library/7kxdx246.aspx


Then after an hour your object will be removed and callback function called.
You can create a separate thread there that will run your lengthy procedure
and reset cache-timer.


George.
 
Which control do you have on the server ?

I don't even know what it means to have a control on the server. If I need
to do it, it's probably safe to say the answer is none.
If you go this route or if you already have this code on an ASP.NET Page
it could be called at a regular interval (using a service such as
http://www.webcron.org/index.php?lang=english).

I'm not sure I understand that page and it doesn't provide a lot of details.
I probably wouldn't want to pay for an ongoing service, and would probably
bag this if there's no way to simply do it from ASP.NET. Also, I have no
idea how I'd write code to respond to this. I can research this further. But
I don't yet have the fundementals.
Also you may want perhaps to elaborate on what those process are supposed
to do as the processing seems to be a bit lengthy.

I indicated they would be lengthy. As I mentioned, it would involve polling
other sites for data and additional tasks.

Thanks.

Jonathan
 
Mark Rae said:
ASP.NET isn't really suited for this sort of thing...

That's how it seems, although I've seen talk of running code from some sort
of timer. Have no idea how that would work though.
And what's the problem there, exactly...?


Heh, if I had guessed at your general response, I'd have been right. Are you
asking me what the problem is with starting an application from scratch
rather than just adding a routine to an existing application that already
has all my types and subroutines?

Thanks.

Jonathan
 
Yes, Ideally to run lengthy jobs you need a separate application.
Windows Service better. Or if you want to do it perfect then I would utilize
a MSMQ to do so.

Basically your application puts a "Job Ticket" into MSMQ and Windows Service
application that monitors this queue picks it up and processes.
The benefits are that this approach is very scalable, and you can have
separate boxes for Web Server and your processing application.
Also you can easily shutdown your processing application to replace it for
example and MSMQ will simply accumulate the "Job Tickets". As soon as you
start your application it will pick it up and process those tickets.

But if you thinking about hosting your application for $10 a month somewhere
then you will not have all those options available.
And the only option you have to "timer" your jobs is to use Cashe object.

George.
 
Yes, Ideally to run lengthy jobs you need a separate application.
Windows Service better. Or if you want to do it perfect then I would
utilize a MSMQ to do so.

Well, since application is the only one of the three that I understand, that
may be a sign that it's the best choice for me. :-)
Basically your application puts a "Job Ticket" into MSMQ and Windows
Service application that monitors this queue picks it up and processes.
The benefits are that this approach is very scalable, and you can have
separate boxes for Web Server and your processing application.
Also you can easily shutdown your processing application to replace it for
example and MSMQ will simply accumulate the "Job Tickets". As soon as you
start your application it will pick it up and process those tickets.

But if you thinking about hosting your application for $10 a month
somewhere then you will not have all those options available.
And the only option you have to "timer" your jobs is to use Cashe object.

Sounds right.

Thanks.

Jonathan
 
1) I meant either this a server on which you can install something
(perhaps your own enterprise server), my personal preference would be then
to create a stand alone application (either a scheduled app, a windows
service, a even a SQL Server job etc...). This way the ASP.NET application
and the out of band process you have to do each work without any
dependencies between them.

Okay, well.... There is no "enterprise server" involved, at least by my
definition. I'm simply paying a hosting company and, in some cases, I'm
paying them very little for multiple sites.

It's sounding more and more like a separate application is the correct
approach. Too bad, I don't have much experience with .NET desktop
applications other than they usually don't work for me.

Thanks.

Jonathan
 
Well, not to nit pick, but I actually have it running now in my ASP.NET app.
I simply perform the task for 100 items in response to clicking a button on
an ASP.NET page.

But I agree this is not ideal. Sounds like a desktop application is really
needed. This is unfortunately, for me, for a couple of reasons. But it may
be doable.

Thanks.

Jonathan
 
Forgot to mention...

Amazon provides those technologies for pennies. It will not be MSMQ but you
could developed similar 'Job ticketing' approach using Amazon's SQS.
So this can be done even with $10 hosting.

Take a look http://aws.amazon.com/

They have SimpleDB, Simple Storage and Simple Queue Service. And they only
charge you for actual use. So while your application is small you paying
almost nothing. And able to scale your applications when needed.


George.
 
Thanks for the info. It's good to know even if I decide it doesn't represent
my best choice at this time.

And looking at the new "Code that runs at regular intervals - How?" thread,
it seems I'm not the only one who wondered about this.

Jonathan
 
Back
Top