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;
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 count = 0;
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();
}