Async updates to DB without BackgroundWorker
-
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?)
-
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?)
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.- From the first page, start a thread (or better if you can use
ThreadPool
) and run theProcess()
method on that. - This method should update the progress to a session variable say
Session["progress"]
- Once the thread started, redirect to the page where progress will be displayed
- Use AJAX and poll the
Session["progress"]
value and show it on the page. For creating progress bar, you can use HTMLDIV
tag with a background color.
You need to pass
HttpContext.Current
to theProcess()
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
- From the first page, start a thread (or better if you can use
-
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.- From the first page, start a thread (or better if you can use
ThreadPool
) and run theProcess()
method on that. - This method should update the progress to a session variable say
Session["progress"]
- Once the thread started, redirect to the page where progress will be displayed
- Use AJAX and poll the
Session["progress"]
value and show it on the page. For creating progress bar, you can use HTMLDIV
tag with a background color.
You need to pass
HttpContext.Current
to theProcess()
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
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! - From the first page, start a thread (or better if you can use
-
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!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
orThreadPool
directly.Best wishes, Navaneeth