使用Updatepanel的AsyncTask?

哟,我创建了一个WebProject来测试是否可以创建一个asyncTask来更新Progressbar:

public partial class AsyncTest : System.Web.UI.Page { private String _taskprogress; private AsyncTaskDelegate _dlgt; protected delegate void AsyncTaskDelegate(); protected void Page_Load(object sender, EventArgs e) { } public String GetAsyncTaskProgress() { return _taskprogress; } public IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData) { _taskprogress = "AsyncTask started at: " + DateTime.Now + ". "; _dlgt = new AsyncTaskDelegate(ReadFile); IAsyncResult result = _dlgt.BeginInvoke(cb, extraData); return result; } public void ReadFile() { try { string Filename = @"D:\Material.txt"; int filelength = TotalLines(Filename); //just reads the lines of the file ProgressBar1.Maximum = filelength; using (StreamReader reader = new StreamReader(Filename)) { string line; int actualfileline = 1; while ((line = reader.ReadLine()) != null) { string test = line; ProgressBar1.Value = actualfileline; actualfileline++; //UpdatePanel1.Update(); <- Doesnt work System.Threading.Thread.Sleep(5); } } } catch (Exception ex) { string exm = ex.Message.ToString(); } } protected void Button1_Click(object sender, EventArgs e) { OnBegin(this, null, null, null); } } 

Aaaand我的aspx代码:(我也有一个脚本管理器)

    

</eo:ProgressBar

调用Updatepanel1.Update()不起作用,因为它说:

您只能调用Update-methoe bevor呈现

那么我怎么能更新呢? 我希望用户看到进度条上的进度 – 因为我想实现更大的文件读取代码片段。 我真的很新的异步编程,所以我不确定你是否有你需要的一切:/

编辑:

好的,现在我在我的页面中创建了一个Webservice:

 [System.Web.Services.WebMethod] [System.Web.Script.Services.ScriptMethod] public string GetProcess() { if (actualfileline != null) { return actualfileline.ToString(); } else { return "0"; } } 

我想把它叫做我的按钮:

  

javascript-part:

 function StartJavaScriptProgress() { var elm = document.getElementById("LabelProgress"); elm.innerText = "Currently Working ..."; setTimeout(WebService(), 1000); }; function WebService() { $.ajax( { type: "POST", url: "AsyncTest.aspx/GetProcess", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert("Worked"); var elm = document.getElementById("ProgressBar1"); elm.value = response.d; }, error: function (jqXHR, textStatus, errorThrown) { alert("Error starting process"); } }) }; 

但它不起作用……

在另一个线程上同步读取文件不会提高Web应用程序的性能,请查看此更多详细信息。 相反,您应该使用像ReadLineAsync这样的自然异步API,并使用RegisterAsyncTask启用HTTP请求的异步完成,如下所示:

 // Make sure to enable asynchronous request processing: // <%@ Page Async="true" ... %> public partial class AsyncTest : System.Web.UI.Page { private String _taskprogress; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { RegisterAsyncTask(new PageAsyncTask(ReadFileAsync)); } public async Task ReadFileAsync() { try { string Filename = @"D:\Material.txt"; int filelength = TotalLines(Filename); //just reads the lines of the file ProgressBar1.Maximum = filelength; using (StreamReader reader = new StreamReader(Filename)) { string line; int actualfileline = 1; while ((line = await reader.ReadLineAsync()) != null) { string test = line; ProgressBar1.Value = actualfileline; actualfileline++; //UpdatePanel1.Update(); <- Doesnt work System.Threading.Thread.Sleep(5); } } } catch (Exception ex) { string exm = ex.Message.ToString(); } } } 

请注意,这仍然不会更新客户端页面中的进度UI。 这样做是为了使当前的 HTTP请求在服务器异步完成。 浏览器仍将等待它。 这样可以提高Web应用程序的可伸缩性,但只有在ReadFileAsync完全完成后才会呈现客户端的Web页面。

如果要在客户端提供进度更新,则应使用AJAX并通过XHR请求启动上述冗长的进程,或者在隐藏的框架内启动。 然后,您可以使用辅助XHR请求跟踪其进度并更新UI。 在ReadFileAsync ,您需要将进度信息保存在会话变量中,例如Session["actualfileline"] = actualfileline 。 您将从辅助XHR请求中访问Session["actualfileline"]