帮助:非常需要! [英] Help : Much Needed !!

查看:78
本文介绍了帮助:非常需要!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长期运行的过程,叫做平衡过程.此过程将更新数据库中的记录.
我有2个单选按钮
1:显示所有记录

2:仅显示不平衡

这些单选按钮用于从数据库中获取数据并将其显示在我的网页上.第二个单选按钮仅显示不平衡记录,顾名思义,第一个显示所有记录.为了平衡不平衡,我具有平衡功能.使用平衡"按钮可以实现此功能.


现在,我的问题是,当我按下平衡"按钮时,该过程冻结了页面,而当我按下任一单选按钮时,发生了切换,但未触发任何事件,即页面未刷新且其中的数据已存在屏幕将一直显示,直到平衡过程结束.

我希望在运行平衡过程的同时,应该能够从显示全部"/仅显示不平衡"屏幕中查看记录的增加/减少情况.现在,我可以看到这些屏幕的初始状态,并且只有在该过程完成之后,才能看到记录数是如何变化的.该进程运行时,屏幕上的内容并没有刷新.我已使用网格视图显示数据.

请帮帮忙!!!!!

更新日期:2012年9月24日

嘿!! BackgroundWorker是一个很大的帮助.但是我有一些问题.因为我在ASP.net中工作,所以在进行平衡过程中每当我按下单选按钮时,都会发生回发,并且平衡过程中止了.对此有什么解决方案吗?...

这是我的代码.

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

推荐答案

将您的平衡过程转移到其他任务中(请参阅BackgroundWorker类 [ ^ ])

此刻,听起来好像您有从Button事件处理程序直接执行的平衡过程的代码,它将冻结UI线程直到完成.
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.


您应该能够当用户按下平衡"按钮时启动一个新线程(甚至更好的是BackgroundWorker).然后,从该线程定期向主线程报告您的进度,然后从那里更新信息.

我建议您看看 .NET线程 [BackgroundWorker [ BackgroundWorker.ReportProgress [ ^ ]
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[^]


这篇关于帮助:非常需要!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆