views:

95

answers:

3

I have a website that allows a user to upload a spreadsheet of items to a database. When the spreadsheet is uploaded it will be assessed for keywords that match individual user interests and email those users an alert to visit the website. However, when the spreadsheet is successfully uploaded the administrator needs to ftp into their server and upload any related images. This can take up to one hour to perform.

I will be executing the email alerts on a new thread and was wondering if, when that thread is called, I can safely call Thread.Sleep(360000) from within it to pause execution for one hour. Would this create a hog on the servers resources? If so is there another way to do this?

I can't create a scheduled task as uploading can happen at any time/day of the week.

+2  A: 

You can't be 100% sure that your application pool won't be recycled, your server rebooted, or any other number of events that could cause your thread to disappear out from under you.

You'll have to have some form of scheduled task, that checks for "things older than X" and runs every minute/couple of minutes and processes any as that approach will allow you to recover from pretty much anything (eg. Server looses power for an hour, your scheduled task catches up on all email alerts that should have happened during that time).

Rob
Good point Rob.
WDuffy
@WDuffy, thanks :)
Rob
@WDuffy, thanks for accepting the answer :) It's gone some way to counteracting the "-1" that someone has plastered 39 of my answers with (I have a *strong* feeling that it's related to an answer to this question that has since been deleted by the answerer) oh well! :)
Rob
+1  A: 

What if the server explodes (replace with more likely event, like "unexpectedly shuts down") after half an hour?
Maybe you should think of a completely different mechanism (store some information in a database, check periodically for requests completed more than one hour ago...).

EDIT: maybe Windows Workflow Foundation could provide you some help for what you are trying to do.

Paolo Tedesco
+2  A: 

Write the necessary information to a queue or file and write a service that processes the queue. It's a much more robust way to handle such tasks. However, I'm curious about your comment:

However, when the spreadsheet is successfully uploaded the administrator needs to ftp into their server and upload any related images. This can take up to one hour to perform.

How do you know the task will be completed in one hour? What signals the completion of the task? Why can't you fire off an email at that point, rather than pre-scheduling the alert? I could be missing something, but it seems like you might not be thinking this through very well.

Chris
Good catch Chris. It can't be guaranteed that the images will be uploaded within the hour. However the client has specified one hour as a wait period and if images are still not uploaded a holder image will be loaded instead.
WDuffy
+1 Queue processing is effective and robust. We had chosen this approach for, let's say, similar needs. An alien automated system was dumping XML file at an FTP location that our service was watching for new files. When new files were present, it processed them and ordered them chronologically. Chronology was a VERY important fact as the process was industrial. The queue was a SQLite database file. Then, another service of our home-made ServiceBroker was dispatching the tasks to the underlying plugins to which the XML message was destined.
Will Marcouiller