Yo, I created a web project to check if it is possible to create asyncTask to update the 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);
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++;
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 my aspx code: (I have a script too)
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" ID="btnDoTask" Text="Do Task" OnClick="Button1_Click" />
<br />
<br />
<eo:ProgressBar ID="ProgressBar1" runat="server" Width="250px"
BackgroundImage="00060301" BackgroundImageLeft="00060302"
BackgroundImageRight="00060303" ControlSkinID="None" IndicatorImage="00060304">
</eo:ProgressBar
</ContentTemplate>
</asp:UpdatePanel>
Calling Updatepanel1.Update () does not work because it says:
You can only render rendering-methoe bevor
So how else can I update it? I want the user to see progress in the progress panel - because I want to implement most of the code that reads files. Im really new to asynchronous programming, so I'm not sure if there is anything you need: /
Edit:
Ok, now I created a web service on my page:
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string GetProcess()
{
if (actualfileline != null)
{
return actualfileline.ToString();
}
else
{
return "0";
}
}
And I want to call it through my button:
<asp:Button runat="server" ID="btnDoTask" Text="Do Task" OnClick="Button1_Click" OnClientClick="StartJavaScriptProgress()" />
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");
}
})
};
But it does not work ...