tags:

views:

139

answers:

3

Using C# ASP.NET I want to program a queue. I want to have X number of a process. When it finishes it should take the next item on the list and process it. I figure the most simple way is to insert and delete it from an SQL database. My problem is:

How do I start this when I add the first item? Do I launch a separate thread? AFAIK every connection to my development environment and server is its own thread? I would need to lock something launch a thread to process the list then unlock and let the thead keep going until its done? So... 1) Should I be launching threads? If so, what kind? (I haven't done any multithreading in C# yet) 2) Should I have a static mutex in my ASP.NET project? And lock it when launching threads? (are static variables still shared across ASP connections/threads correct?) Or should I not be doing this and launch them a different way?

NOTE: I may want to launch 2 processes instead of 1 and I may want to launch other processes for other things (example 2 FFmpeg + 5 ImageMagick.)

+2  A: 
free-dom
A: 

First off, it sounds like what you really want is a Windows Service.

That said, if you're committed to using ASP.NET for this, the following might work:

  • Make a single .aspx page whose sole purpose is to process one unit of work.
  • Have another page (HTML will do) that uses JavaScript to asynchronously load your first page (using something like jQuery's $.load() method).
  • When $.load() returns, you'll know the job is complete and you can make another request to process the next job.
  • Optionally, you can put something into the returned page to indicate whether or not there are any remaining units of work left in the queue. This would allow you to throttle back on the client-side when there's not any work to be done.

Client-Side Example

<script type="text/javascript">
    $(document).ready(function() {
        processJob();
    });

    function processJob() {
        $('#result').load("ProcessOneJob.aspx", function() {
            // called when ProcessOneJob.aspx comes back
            processJob();
        });
    }
</script>

Pros to this approach:

  • Relatively simple to implement
  • No need to deal with threads/locking
  • No service to install

Cons

  • Relies entirely on having a machine somewhere with a browser open, pointing at this page.
Chris Zwiryk
A: 

A brainstorm answer. I don't know if it will work.

The issue is threads get terminated when a request is finished so create a thread outside of a request in Application_Start to avoid the problem.

To keep everything organized and simple; Have a master class that acts as a utility controlling the amount of each process you would like to launch and to do the actual launching. Call the class in void Application_Start(object sender, EventArgs e) to create the initial master thread which will launch any process (or threads) you'll need. Allow it to put itself to sleep and use the utility methods to wake it up and pass messages. Then handle the rest as needed :)

acidzombie24
This approach is not at all what a web server was designed for. If you're going to go through all the trouble of managing threads, why not just create your own service?
Chris Zwiryk
@chris: I dont know what the difference is, how my own service would benefit me nor how to create it. Thus this question. So web service is different from windows service? i am very new.
acidzombie24