【问题标题】:Change process value from codebehind?从代码隐藏更改过程值?
【发布时间】:2012-06-08 10:43:26
【问题描述】:

我使用 ASP.NET

我想在数据库工作时从代码隐藏中向用户显示百分比。

我认为问题就在这里,当我从cs percentege WORKS FINE 调用这个函数时!

  protected void btnUpdate_Click(object sender, EventArgs e)
  {
        System.Threading.Thread.Sleep(5000);
  }

但我真正的代码是连接数据库并插入 300 - 1000 行! 当它工作时,服务器光标图标变为忙碌图标,所以它冻结了,我无法设置我的百分比值。

请帮助...

我有一个网络服务:

    public double CommittedCount = 0;

    [WebMethod]
    public string ShowPercentage()
    {
        return ((CommittedCount / FinishCount) * 100 ).ToString();
    }

    [WebMethod]
    public void SetCommittedCount(double committedCount)
    {
        CommittedCount = committedCount;
    }


    public double FinishCount
    {
        get
        {
                if(Glob.ExcelDataSet.Tables[0].Rows.Count > 0)
                    return Glob.ExcelDataSet.Tables[0].Rows.Count;
                return 1;

        }
    }

我有一个ajaxcall函数:

function updateProgress() {
        $.ajax({
            type: "POST",
            url: '<%=ResolveUrl("~/processCalculator.asmx/ShowPercentage") %>',
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            success: function (msg) {
                // TODO: revert the line below in your actual code
                //$("#progressbar").progressbar("option", "value", msg.d);
                $(".percentage").text(msg.d);
                if (msg.d < 100) {
                    setTimeout(updateProgress, 100);
                }
            }
        });

    }

我在点击时调用了 updateProgress 按钮:

<asp:Button Text="Sorgula" ID="btnAction" class="button_action" OnClick="Action_Click" Width="80px" runat="server" />

        $(".button_action").click(function () {
            var action_ = $(this).attr("value");
            var ExcelRowCount = $('#<%=ExcelActionsIsAvaibleRowCount.ClientID %>').val();
            if (ExcelRowCount != "") {
                if (confirm(ExcelRowCount + " kayıt için [" + action_ + "] aksiyonu gerçekleştirilecek?")) {
                    setTimeout(updateProgress, 100);
                    return true;
                }
                else 
                {
                    return false;
                }
            }
        });

我的 Cs 动作代码 protected void btnAction_Click(对象发送者,EventArgs e) {

...
...
for (int rowIndex = 1; rowIndex < Glob.ExcelDataSet.Tables[0].Rows.Count; rowIndex++)
{
   ...     
   ...                

   aksiyon_sonucu_ = action.InsertRow(
   Glob.ExcelDataSet.Tables[0].Rows[rowIndex], DegistirilebilirKolonlar, true);

  // my persentage
  processCalculater pCal = new processCalculater();
  pCal.SetCommittedCount(rowIndex);

  ...
  ...

}

【问题讨论】:

    标签: asp.net ajax percentage


    【解决方案1】:

    您需要使用 PageAsyncTask(或其他一些多线程)。

    这是来自 MSDN 的关于用法的文章,其中还有一个显示进度指示器的示例。

    http://msdn.microsoft.com/en-us/library/system.web.ui.pageasynctask.aspx

    多线程的其他选项是:

    ThreadPool.QueueUserWorkItem(s => System.Threading.Thread.Sleep(5000))
    

    new Thread(() => System.Threading.Thread.Sleep(5000)){ IsBackground = true }.Start() 
    

    我们的想法是将您长时间运行的任务放入后台线程(使用上述任何技术)。传入您的上下文(会话)以保存您的进度。然后使用 AJAX 和 WebMethod,您可以访问会话数据并显示后台线程的进度。

    【讨论】:

    • 我怎样才能调用更多这个 PageAsyncTask ?它只有一个电话,我想不通。我想每秒都打这个电话,我该怎么做?
    • 您不想每秒调用 PageAsyncTask,只需要调用一次。请参阅我的答案中添加的注释。
    • 我如何停止这个线程?新线程(() => w.ActionStart(ac)) { IsBackground = true }.Start();
    • @Mennan - 阅读这篇关于启动和终止线程的文章。 msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.80).aspx
    猜你喜欢
    • 2010-11-19
    • 2013-12-14
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2011-04-23
    • 2011-01-21
    相关资源
    最近更新 更多