Net中的长期运行过程 [英] Long Running Process in Asp,Net

查看:115
本文介绍了Net中的长期运行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的

我有一个Gridview,它包含一个teamplate链接按钮列.链接按钮单击evnets需要很长时间.

在我的代码中,我使用线程来管理长时间的过程.但是,如果线程完成了该过程,则GridView不会更新其状态. 线程执行后如何更新gridview链接按钮文本? .

我尝试重新绑定gridview,但是它抛出此错误脚本控件在预渲染后可能未注册

我的代码如下

Dears

I have a Gridview and it contain a teamplate link button column .The link button click evnets take a long time process .

In my code i using a Thread to manage the long time process. But if the thread complete the process the GridView is not updating it''s status. How i update the gridview link button text after the thread execute ? .

I tried to re-bind the gridview but it throw this error script controls may not be registered after prerender

My code as follows

The following code is working fine and the only problem is that after completing each thread i calling<br />
 a method called "PTACalculationEnd"  and in this method i updating link button grid template column text will be changed to "Already Calculated"   But.. it is not updating the Grid view and  no error .




网格视图Itemcommand事件





Grid view Itemcommand event


protected void GrdPTAConfigure_ItemCommand(object sender, GridCommandEventArgs e)
       {

           GridDataItem item = (GridDataItem)e.Item;
           LinkButton button = item["tclnkConfigure"].Controls[0] as LinkButton;
           button.Text = "Calculating...";
           button.Font.Underline = false;
           button.ForeColor = System.Drawing.Color.LightSeaGreen;
           button.Enabled = false;

           ((e.Item as GridDataItem)["chkSelectPTA"].Controls[0] as CheckBox).Enabled = false;

           Thread t = new Thread( new ThreadStart(delegate()
           {
               DoPTACalculations(e, PTACalculationEnd);
           }));

           t.Start();


       }









private void DoPTACalculations(GridCommandEventArgs e, Callback callback)
       {


           if (e.CommandName == "Calculate")
           {
              //Do a Long Process Task

            }
        }







public delegate void Callback();

      public void PTACalculationEnd()
      {
              LinkButton button = item["tclnkConfigure"].Controls[0] as LinkButton;

              if (IsAlreadyConfigured(ddlBranch.SelectedValue.ToString())
              {

                  button.Text = "Already Calaculated";
                  button.Font.Underline = false;
                  button.ForeColor = System.Drawing.Color.Green;
                  button.Enabled = true;

              }

              else
              {

                  button.Text = "Calculate";
                  button.Font.Underline = true;
                  button.ForeColor = System.Drawing.ColorTranslator.FromHtml("#0060d0");
                  button.Enabled = true;
              }

          }



      }

推荐答案

尝试更改此内容

Try changing this

Thread t = new Thread( new ThreadStart(delegate()
            {
                DoPTACalculations(e, PTACalculationEnd);
            }));

            t.Start();



对此




to this


DoPTACalculations(e, PTACalculationEnd);



在示例中,您显示页面没有任何机制可以确定线程何时完成.我想象服务器在线程完成之前发送响应.


根据您的评论,如果您需要较长的处理线程来处理该线程,则需要管理这些线程,以便在尝试修改输出之前响应不会结束.我建议这样做是为了使您能够看到它没有显示预期的更改,因为服务器在线程仍在执行时发送页面响应.我想像一下,当触发ItemCommand后重新加载页面时,按钮的文本显示为正在计算..."并被禁用.一旦将响应发送回客户端,线程将无法发送它尝试进行的更改.

这是您应该查看的一些链接

ASP.NET中的多线程 [使用ASP.NET的异步功能来创建用于显示处理消息的简单,可重用的页面 [



In the sample you show you do not have any mechanism for the page to determine when your thread has completed. I imagine that the server sends the response before your thread has finished.


Per your comment, if you require long processing thread to handle this then you need to manage the threads so the response does not end before you are done trying to modify the output. I suggested that just so you would be able to see that it is not showing the changes you expected because the server sends the page response while your threads are still executing. I would imaging that when your page reloads after triggering the ItemCommand your button''s text says "Calculating..." and is disabled. Once the response is sent back to the client there is no way for the thread to send out the changes it trys to make.

Here are some links you should check out

Multi-Threading in ASP.NET[^]

Use the Asynchronous Power of ASP.NET to Create a Simple, Reusable Page for Displaying a Processing Message[^]


这篇关于Net中的长期运行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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