task progress c# asp and response code example
Example 1: Reporting Progress from Async Tasks c#
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgressBarDemo
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public void DoSomething(IProgress<int> progress)
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(100);
if (progress != null)
progress.Report(i);
}
}
private async void btnStart_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
var progress = new Progress<int>(percent =>
{
progressBar1.Value = percent;
});
await Task.Run(() => DoSomething(progress));
}
}
}
Example 2: Reporting Progress from Async Tasks c#
public async void StartProcessingButton_Click(object sender, EventArgs e)
{
var progress = new Progress<int>(percent =>
{
textBox1.Text = percent + "%";
});
await Task.Run(() => DoProcessing(progress));
textBox1.Text = "Done!";
}
public void DoProcessing(IProgress<int> progress)
{
for (int i = 0; i != 100; ++i)
{
Thread.Sleep(100);
if (progress != null)
progress.Report(i);
}
}