Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Reporting Progress from Async Tasks c#

public async void StartProcessingButton_Click(object sender, EventArgs e)
{
  // The Progress<T> constructor captures our UI context,
  //  so the lambda will be run on the UI thread.
  var progress = new Progress<int>(percent =>
  {
    textBox1.Text = percent + "%";
  });

  // DoProcessing is run on the thread pool.
  await Task.Run(() => DoProcessing(progress));
  textBox1.Text = "Done!";
}

public void DoProcessing(IProgress<int> progress)
{
  for (int i = 0; i != 100; ++i)
  {
    Thread.Sleep(100); // CPU-bound work
    if (progress != null)
      progress.Report(i);
  }
}
Comment

Reporting Progress from Async Tasks c#

public async Task DownloadFileAsync(string fileName, IProgress<int> progress)
{
  using (var fileStream = ...) // Open local file for writing
  using (var ftpStream = ...) // Open FTP stream
  {
    while (true)
    {
      var bytesRead = await ftpStream.ReadAsync(...);
      if (bytesRead == 0)
        return;
      await fileStream.WriteAsync(...);
      if (progress != null)
        progress.Report(bytesRead);
    }
  }
}
Comment

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));
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: wpf app transparent background with blurred image affect 
Csharp :: calculate string length vs pixels c# 
Csharp :: logical operators in c# 
Csharp :: calculate textbox value c# 
Csharp :: float into int unoity 
Csharp :: shuffle array c# 
Csharp :: dataannotations for currency in c# 
Csharp :: unity get quaternion z 
Csharp :: dotnet core clickable row 
Csharp :: c# null accessor 
Csharp :: ef save changes 
Csharp :: c# different getter setter types 
Csharp :: camera follow player unity 
Csharp :: constant interpolated string 
Csharp :: how to change the color of a single line of code in c# 
Csharp :: Get single listView SelectedItem 
Csharp :: c# sequential struct char array fixed size 
Csharp :: cannot convert from group method to threadstart C# 
Csharp :: asp.net list size 
Csharp :: Running C# Example 
Csharp :: List foreach 
Csharp :: how to get text color alpha unity 
Csharp :: c# Lucene search - build index 
Csharp :: print all string in textbox in array c# 
Csharp :: convert relative path to physical path c# 
Csharp :: c# use meditor from service 
Csharp :: Untiy particle system play 
Csharp :: CS0234 compile error c# unity fix scene managment 
Csharp :: Unity FPS camera z axis rotating problem 
Csharp :: Modify middleware response c# .net 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =