question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked Viorel-1 commented

Calling Task and report progress from there with IProgress

From my Async function i am using IProgress interface to write status into textbox but not getting expected output. where i made the mistake in my code ? i am using winform project.

 public async Task<int> Getdata(int i)
         {
             IProgress<string> progress = new Progress<string>(str =>
             {
                 textBox1.Text = str;
             });
    
             await Task.Delay(90000);
             progress.Report("Task completed "+i.ToString());
             //return Task.FromResult(10);
             return 10;
         }
    
         private async void btnTask1_Click(object sender, EventArgs e)
         {
             List<Task<int>> tasks = new List<Task<int>>();
    
             for (int i = 0; i < 5; i++)
             {
                 tasks.Add(Getdata(i + 1));
             }
    
             var result = await Task.WhenAll(tasks);
         }


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


It seems to work. If possible, publish a full sample project or show more details to reproduce the problem.

0 Votes 0 ·

I update the code and now it is working as expected. answer posted. please see and share your comments. thanks

0 Votes 0 ·

What mistakes were fixed in Getdata and btnTask1_Click to make them work?

0 Votes 0 ·

1 Answer

TZacks-2728 avatar image
0 Votes"
TZacks-2728 answered

See this code and it is working fine.

 public async Task<int> Getdata(int i)
         {
             IProgress<string> progress = new Progress<string>(str =>
             {
                 textBox1.Text = str;
             });
    
             await Task.Delay(90000);
             progress.Report("Task completed "+i.ToString());
             return 10;
         }
    
         private async void btnTask1_Click(object sender, EventArgs e)
         {
             List<Task<int>> tasks = new List<Task<int>>();
    
             for (int i = 0; i < 5; i++)
             {
                 tasks.Add(Getdata(i + 1));
             }
    
             var result = await Task.WhenAll(tasks);
         }
    
         private async void btnTask2_Click(object sender, EventArgs e)
         {
             var tasks = new List<Task<int>>();
             for (int ctr = 1; ctr <= 10; ctr++)
             {
                 int baseValue = ctr;
                 tasks.Add(Task.Factory.StartNew(b => (int)b * (int)b, baseValue));
             }
    
             var results = await Task.WhenAll(tasks);
    
             int sum = 0;
             for (int ctr = 0; ctr <= results.Length - 1; ctr++)
             {
                 var result = results[ctr];
                 textBox1.Text += result.ToString() + ((ctr == results.Length - 1) ? "=" : "+");
                 sum += result;
             }
             textBox1.Text += sum.ToString();
         }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.