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. Async updates to DB without BackgroundWorker

Async updates to DB without BackgroundWorker

Scheduled Pinned Locked Moved ASP.NET
databasecsharpasp-netquestionannouncement
4 Posts 2 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.
  • S Offline
    S Offline
    student_rhr
    wrote on last edited by
    #1

    Hi, In my ASP.NET application, I have a situation where I am doing a lot of computing and database reads/inserts/updates. I needed to gather statistics on the processing records. This info is actually displayed on another webpage. Processing routine looks something like this:

    public void Process()
    {
        int recordCount = 0;
        ProgressHelper.Start();
        while(processing)    
        {
            //Actual processing logic
            recordCount++;
    
            //Every 10 processed records, update the progress table
            if(recordCount % 10 == 0)
            {
               ProgressHelper.UpdateProcessedRecordCount(recordCount); 
            }
        }
        ProgressHelper.End();
    }
    

    These database calls to update the progress add additional delay. I was wondering if there is a way to actually execute all the ProgressHelper stuff asynchronously (without using background worker)? (perhaps delegates ... but are delegates really async?)

    N 1 Reply Last reply
    0
    • S student_rhr

      Hi, In my ASP.NET application, I have a situation where I am doing a lot of computing and database reads/inserts/updates. I needed to gather statistics on the processing records. This info is actually displayed on another webpage. Processing routine looks something like this:

      public void Process()
      {
          int recordCount = 0;
          ProgressHelper.Start();
          while(processing)    
          {
              //Actual processing logic
              recordCount++;
      
              //Every 10 processed records, update the progress table
              if(recordCount % 10 == 0)
              {
                 ProgressHelper.UpdateProcessedRecordCount(recordCount); 
              }
          }
          ProgressHelper.End();
      }
      

      These database calls to update the progress add additional delay. I was wondering if there is a way to actually execute all the ProgressHelper stuff asynchronously (without using background worker)? (perhaps delegates ... but are delegates really async?)

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      What is ProgressHelper? BackgroundWorker is a class designed for windows applications not web applications. So using it in a web application is not a good idea. If I understood your question, you need to do a time consuming task on background and show a progress bar on another ASP.NET page, correct? If yes, follow the below steps.

      1. From the first page, start a thread (or better if you can use ThreadPool) and run the Process() method on that.
      2. This method should update the progress to a session variable say Session["progress"]
      3. Once the thread started, redirect to the page where progress will be displayed
      4. Use AJAX and poll the Session["progress"] value and show it on the page. For creating progress bar, you can use HTML DIV tag with a background color.

      You need to pass HttpContext.Current to the Process() method because it is thread specific and a worker thread won't have access to it. Use this context to access the session. Hope that helps :)

      Best wishes, Navaneeth

      S 1 Reply Last reply
      0
      • N N a v a n e e t h

        What is ProgressHelper? BackgroundWorker is a class designed for windows applications not web applications. So using it in a web application is not a good idea. If I understood your question, you need to do a time consuming task on background and show a progress bar on another ASP.NET page, correct? If yes, follow the below steps.

        1. From the first page, start a thread (or better if you can use ThreadPool) and run the Process() method on that.
        2. This method should update the progress to a session variable say Session["progress"]
        3. Once the thread started, redirect to the page where progress will be displayed
        4. Use AJAX and poll the Session["progress"] value and show it on the page. For creating progress bar, you can use HTML DIV tag with a background color.

        You need to pass HttpContext.Current to the Process() method because it is thread specific and a worker thread won't have access to it. Use this context to access the session. Hope that helps :)

        Best wishes, Navaneeth

        S Offline
        S Offline
        student_rhr
        wrote on last edited by
        #3

        Thanks Navaneeth. ProgressHelper is my internal DAL responsible for udpating the progress table (sorry I should have explained). I agree with you that BackgroundWorker is a bad idea that's why I was wondering if there might be a better way to accomplish this. Also, what are your thoughts about Async Delegates? Have you seen any adverse effects of using them in ASP.NET? Appreciate your hlep!

        N 1 Reply Last reply
        0
        • S student_rhr

          Thanks Navaneeth. ProgressHelper is my internal DAL responsible for udpating the progress table (sorry I should have explained). I agree with you that BackgroundWorker is a bad idea that's why I was wondering if there might be a better way to accomplish this. Also, what are your thoughts about Async Delegates? Have you seen any adverse effects of using them in ASP.NET? Appreciate your hlep!

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          student_rhr wrote:

          I agree with you that BackgroundWorker is a bad idea that's why I was wondering if there might be a better way to accomplish this.

          See the point 1 in my first reply.

          student_rhr wrote:

          Also, what are your thoughts about Async Delegates? Have you seen any adverse effects of using them in ASP.NET?

          Asynchronous pattern will make your code unnecessarily complicated. So I'd stay away from it unless there is a strong need for using it. As I said, work with Thread or ThreadPool directly.

          Best wishes, Navaneeth

          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