Click here to Skip to main content
12,359,238 members (33,877 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: .NET threads
I have a long running process called as the balancing process. This process updates the records in the database.
I have 2 radio buttons
1: Show all records

2: Show only imbalances

These radio buttons are used to fetch the data fromt he database and display them on my web page. The 2nd radio button shows only the imbalance records as the name suggests and the 1st one shows all the records. In order to balance the Imbalances I have the balancing function. This function is implemented using the Balance button.


Now, My problem is that when i press the balance button, the process freezes up the page and when i press either of the radio buttons the toggling takes place but no event is fired i.e the page does not refresh and the data which is there on the screen is seen as it is until the balancing process ends.

I want that while the balancing process is running, I should be able to see how the records increase/decrease from the ‘Show All’/ ‘Show Only Imbalances’ screens. Right now, I can see the initial state of these screens ,and ONLY after the process is completed, then I can see how the number of records changed. The screens are not refreshing with what’s happening while the process is running. I have used a grid view to display the data.

please help guys!!!!!

Updated On : 24 Sep 2012

Hey!! BackgroundWorker is a great help.. But i have some problems. Since i am working in ASP.net, whenever i press the radio button while the balance process is going on, there is a postback and i thing the balancing process gets aborted. Do have any solution for this....?

here is my code..
public partial class Balance : System.Web.UI.Page
{
   SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=RoomManagementdb;Integrated Security=SSPI");
   BackgroundWorker backwork = new BackgroundWorker();
 
   protected void Page_Load(object sender, EventArgs e)
   {
      backwork.WorkerReportsProgress = true;
      backwork.WorkerSupportsCancellation = true;
      backwork.DoWork += new DoWorkEventHandler(backwork_DoWork);
      backwork.ProgressChanged += new ProgressChangedEventHandler(backwork_ProgressChanged);
      backwork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backwork_RunWorkerCompleted);
      DataBind();
   }
 
   public void LoadGrid()
   {
      SqlDataAdapter adp;
      DataTable dt = new DataTable();
      if (rdbShowAll.Checked == true)
      {
         adp = new SqlDataAdapter("Select * from RoomDetails", con);
         adp.Fill(dt);
         balancegrid.DataSource = dt;
      }
      else if (rdbShowOnlyImbal.Checked == true)
      {
         adp = new SqlDataAdapter("Select * from RoomDetails where Status='Closed'", con);
         adp.Fill(dt);
         balancegrid.DataSource = dt;
      }
      DataBind();
   }
 
   protected void rdbShowOnlyImbal_CheckedChanged(object sender, EventArgs e)
   {
      rdbShowAll.Checked = false;
      LoadGrid();
   }
 
   protected void rdbShowAll_CheckedChanged(object sender, EventArgs e)
   {
      rdbShowOnlyImbal.Checked = false;
      //LoadGrid();
      Response.Write("HI There!!");
   }
 
   protected void btn_balance_Click(object sender, EventArgs e)
   {
      backwork.RunWorkerAsync();
   }
 
   protected void backwork_DoWork(object sender, DoWorkEventArgs e)
   {
      this.backwork = sender as BackgroundWorker;
      int iCount = new Random().Next( 20, 50 );
      for (int i = 0; i < iCount; i++)
      {
         backwork.ReportProgress(Convert.ToInt32(i * (100.0 / iCount)));
      }
 
      if (backwork.CancellationPending == true)
      {
         e.Cancel = true;
      }
      else
      {
         e.Result = balancing(backwork);
      }
      this.Label1.Text = e.Result.ToString();
   }
 
   protected void backwork_ProgressChanged(object sender, ProgressChangedEventArgs e)
   {
      this.Label1.Text = (e.ProgressPercentage.ToString() + "%");
   }
 
   protected void backwork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
   {
      if (e.Cancelled == true)
      {
         Label1.Text = "Cancelled";
      }
      else
         Label1.Text = "Done";
   }
 
   public int balancing(BackgroundWorker work)
   {
      //int number = 0;
      int count = 0;
      //con.Open();
      //SqlCommand cmd = new SqlCommand("Update RoomDetails Set Status='Open' where Status='Closed'", con);
      //number = cmd.ExecuteNonQuery();
      
      for (int i = 0; i < 1000000000; i++)
      {
         count++;
      }
      
      con.Close();
      return count;
   }
 
   protected void btn_cancel_Click(object sender, EventArgs e)
   {
      if (backwork.WorkerSupportsCancellation == true)
      backwork.CancelAsync();
   }
Posted 17-Sep-12 1:24am
Updated 25-Sep-12 2:16am
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Move your balancing process into a different task (see BackgroundWorker class[^])

At the moment, it sounds as if you have the code for the balancing process directly executing from the Button event handler, which will freeze the UI thread until it is complete.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You should be able to start a new thread (even better a BackgroundWorker) when your user presses the Balance button. Then, from that thread, report your progress periodically to the main thread, and from there update the info.

I suggest you take a look into .NET threading[^], and more specifically for your problem BackgroundWorker[^] and BackgroundWorker.ReportProgress[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 25 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100