Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Background worker thread to process many request asynchronously

Background worker thread to process many request asynchronously

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesigndata-structuresquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hamed Musavi
    wrote on last edited by
    #1

    I'm very new to Asp.net. Right now I'm working on a website (implementing registration particularly). Users need to provide their email address and an activation email must be sent to their email box. On desktop in such situations(knowing that many users might simultaneously request registration) I would've created a thread that waits on a thread-safe queue to take an email address from that queue and send activation URL to taken address. This way UI thread would just need to put received email into queue and show a message that an email is being sent. In Asp.net, Can I have a worker thread that runs as long as my application runs(maybe created in Application_Start) and a queue. How reliable is this solution in asp.net? What are alternatives. Thanks.

    "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
     - I wish I knew who is this quote from

    E T D S 4 Replies Last reply
    0
    • H Hamed Musavi

      I'm very new to Asp.net. Right now I'm working on a website (implementing registration particularly). Users need to provide their email address and an activation email must be sent to their email box. On desktop in such situations(knowing that many users might simultaneously request registration) I would've created a thread that waits on a thread-safe queue to take an email address from that queue and send activation URL to taken address. This way UI thread would just need to put received email into queue and show a message that an email is being sent. In Asp.net, Can I have a worker thread that runs as long as my application runs(maybe created in Application_Start) and a queue. How reliable is this solution in asp.net? What are alternatives. Thanks.

      "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
       - I wish I knew who is this quote from

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Your best bet is to put the requests for a registration email into a database or other table and then have a non-ASP.NET service respond to those pending requests. (You could also use MSMQ instead of a db for this).

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      H E 2 Replies Last reply
      0
      • H Hamed Musavi

        I'm very new to Asp.net. Right now I'm working on a website (implementing registration particularly). Users need to provide their email address and an activation email must be sent to their email box. On desktop in such situations(knowing that many users might simultaneously request registration) I would've created a thread that waits on a thread-safe queue to take an email address from that queue and send activation URL to taken address. This way UI thread would just need to put received email into queue and show a message that an email is being sent. In Asp.net, Can I have a worker thread that runs as long as my application runs(maybe created in Application_Start) and a queue. How reliable is this solution in asp.net? What are alternatives. Thanks.

        "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
         - I wish I knew who is this quote from

        T Offline
        T Offline
        T M Gray
        wrote on last edited by
        #3

        How many simultaneous registrations are you expecting? Unless you are expecting more than 100 per second, I don't think you need to worry about threading. Or rather than worry about threading in your ASP.Net app, make a web service that handles sending the e-mail and make asynchronous calls to the web service from your ASP.Net site.

        H 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          Your best bet is to put the requests for a registration email into a database or other table and then have a non-ASP.NET service respond to those pending requests. (You could also use MSMQ instead of a db for this).

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          H Offline
          H Offline
          Hamed Musavi
          wrote on last edited by
          #4

          Thanks. Nice solution but Windows service will need to query database server every now and then(polling) to see if any new email is inserted. Maybe inserting an item into database and a notification message in MSMQ lead to better performance. Thanks anyway.

          "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
           - I wish I knew who is this quote from

          1 Reply Last reply
          0
          • T T M Gray

            How many simultaneous registrations are you expecting? Unless you are expecting more than 100 per second, I don't think you need to worry about threading. Or rather than worry about threading in your ASP.Net app, make a web service that handles sending the e-mail and make asynchronous calls to the web service from your ASP.Net site.

            H Offline
            H Offline
            Hamed Musavi
            wrote on last edited by
            #5

            T M Gray wrote:

            more than 100 per second

            Wow! That's too much. I expected much less. I don't think it ever happens! Being new to asp.net I hardly can estimate resource usages per user. Well I'm far less concerned now. Thank you. :)

            "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
             - I wish I knew who is this quote from

            1 Reply Last reply
            0
            • H Hamed Musavi

              I'm very new to Asp.net. Right now I'm working on a website (implementing registration particularly). Users need to provide their email address and an activation email must be sent to their email box. On desktop in such situations(knowing that many users might simultaneously request registration) I would've created a thread that waits on a thread-safe queue to take an email address from that queue and send activation URL to taken address. This way UI thread would just need to put received email into queue and show a message that an email is being sent. In Asp.net, Can I have a worker thread that runs as long as my application runs(maybe created in Application_Start) and a queue. How reliable is this solution in asp.net? What are alternatives. Thanks.

              "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
               - I wish I knew who is this quote from

              D Offline
              D Offline
              David Mujica
              wrote on last edited by
              #6

              Create yourself a Windows Service that handles these "background" type requests, it makes life so much easier for debugging and troubleshooting. My basic design is to use a directory where the service "listens" (FileSystemWatcher) for an XML file to be dropped. It then picks up the file and performs the necessary request. Once the request is complete, it moves the XML file to either an "Archive" or "Error" folder. I use the Event Log to track basic message status, but you can devise any type of logging system you want. In my system, I know that there aren't going to be lots of requests, so this is a single threaded job, very simple, yet robust. I'm all for keeping things simple and reliable. :cool:

              A 1 Reply Last reply
              0
              • H Hamed Musavi

                I'm very new to Asp.net. Right now I'm working on a website (implementing registration particularly). Users need to provide their email address and an activation email must be sent to their email box. On desktop in such situations(knowing that many users might simultaneously request registration) I would've created a thread that waits on a thread-safe queue to take an email address from that queue and send activation URL to taken address. This way UI thread would just need to put received email into queue and show a message that an email is being sent. In Asp.net, Can I have a worker thread that runs as long as my application runs(maybe created in Application_Start) and a queue. How reliable is this solution in asp.net? What are alternatives. Thanks.

                "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
                 - I wish I knew who is this quote from

                S Offline
                S Offline
                si24803
                wrote on last edited by
                #7

                Hi, I believe that the answer is by using the singleton pattern. Just create a singleton class for the process queue (with the put method and a permanently alive thread or threads that block when the queue is empty and resumes execution when the queue receives a put) and use it in your ASP application.

                H 1 Reply Last reply
                0
                • S si24803

                  Hi, I believe that the answer is by using the singleton pattern. Just create a singleton class for the process queue (with the put method and a permanently alive thread or threads that block when the queue is empty and resumes execution when the queue receives a put) and use it in your ASP application.

                  H Offline
                  H Offline
                  Hamed Musavi
                  wrote on last edited by
                  #8

                  si24803 wrote:

                  using the singleton pattern

                  Honestly, I prefer to avoid any static thing as much as I can. Singleton in my opinion is just the same static functions + a constructor. The constructor adds benefits but using static functions in a multi-threaded world is tricky. Keeping track of all shared resources during application lifetime is hard indeed. Don’t get me wrong. I don’t say I totally avoid them but I will as much as I can.

                  si24803 wrote:

                  a permanently alive thread or threads that block when the queue is empty

                  That was my first question. Answers were great. While that fits very well in desktop applications, I’ve learned that it's not the best possible solution in a web application. I think best possible solution was given by T M Gray; first ensure if it’s really needed. If so, then have a separate Win/Web Service to do background probably time-intensive jobs. Use an IPC mechanism (WCF netpipes maybe) or RPC or SOAP messages, in case of a WebService, to inform the Win/Web Service asynchronously that a new email is to be processed.

                  "I hope you live a life you're proud of. If you find that you're not, I hope you have the strength to start all over again."    
                   - I wish I knew who is this quote from

                  modified on Monday, August 23, 2010 7:29 AM

                  1 Reply Last reply
                  0
                  • E Ennis Ray Lynch Jr

                    Your best bet is to put the requests for a registration email into a database or other table and then have a non-ASP.NET service respond to those pending requests. (You could also use MSMQ instead of a db for this).

                    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                    E Offline
                    E Offline
                    ely_bob
                    wrote on last edited by
                    #9

                    This approach works very well. I recommend it on grounds of reliability.

                    I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...

                    1 Reply Last reply
                    0
                    • D David Mujica

                      Create yourself a Windows Service that handles these "background" type requests, it makes life so much easier for debugging and troubleshooting. My basic design is to use a directory where the service "listens" (FileSystemWatcher) for an XML file to be dropped. It then picks up the file and performs the necessary request. Once the request is complete, it moves the XML file to either an "Archive" or "Error" folder. I use the Event Log to track basic message status, but you can devise any type of logging system you want. In my system, I know that there aren't going to be lots of requests, so this is a single threaded job, very simple, yet robust. I'm all for keeping things simple and reliable. :cool:

                      A Offline
                      A Offline
                      AllenRogerMarshall
                      wrote on last edited by
                      #10

                      We have had a lot of luck with this approach, and it has the salient benefit that the service can be administered simply from the administrative tools on the server, and restarted as needed. Further, it can have a very simple console monitoring capability that lets you see what it is up to in real time. Also, the very same service can interact with any sort of application, not just a web submittal app...

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups