Click here to Skip to main content
15,897,704 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to report progress from Async function Pin
Richard Deeming29-Apr-21 21:15
mveRichard Deeming29-Apr-21 21:15 
You haven't described your expected result, nor told us what "not working" means.

Bear in mind your task will all complete at roughly the same time, 90 seconds after you start them. You will only notice a single update to the textbox.

It's generally better to avoid async void methods[^]. And I'd be inclined to create a single Progress<T> instance and pass it in to the Getdata method.
C#
public async Task GetData(int i, IProgress<string> progress)
{
    progress.Report($"Task created {i}");
    await Task.Delay(60000 + i * 1000);
    progress.Report($"Task completed {i}");
}

private async Task Run(IProgress<string> progress)
{
    List<Task> tasks = new List<Task>();
    for (int i = 0; i < 5; i++)
    {
        tasks.Add(GetData(i + 1, progress));
    }
    
    await Task.WhenAll(tasks);
}

private void btnTask1_Click(object sender, EventArgs e)
{
    IProgress<string> progress = new Progress<string>(str => textBox1.Text = str);
    _ = Run(progress);
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: How to report progress from Async function Pin
Mou_kol29-Apr-21 22:10
Mou_kol29-Apr-21 22:10 
GeneralRe: How to report progress from Async function Pin
Richard Deeming30-Apr-21 1:45
mveRichard Deeming30-Apr-21 1:45 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 3:18
Mou_kol1-May-21 3:18 
GeneralRe: How to report progress from Async function Pin
Dave Kreskowiak1-May-21 5:29
mveDave Kreskowiak1-May-21 5:29 
AnswerRe: How to report progress from Async function Pin
OriginalGriff30-Apr-21 19:56
mveOriginalGriff30-Apr-21 19:56 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 3:15
Mou_kol1-May-21 3:15 
GeneralRe: How to report progress from Async function Pin
OriginalGriff1-May-21 4:21
mveOriginalGriff1-May-21 4:21 
GeneralRe: How to report progress from Async function Pin
Mou_kol1-May-21 6:42
Mou_kol1-May-21 6:42 
AnswerRe: How to report progress from Async function Pin
Eddy Vluggen3-May-21 6:25
professionalEddy Vluggen3-May-21 6:25 
QuestionC# How to speed up for loop with large data iteration Pin
Mou_kol29-Apr-21 2:40
Mou_kol29-Apr-21 2:40 
AnswerRe: C# How to speed up for loop with large data iteration Pin
Richard Andrew x6429-Apr-21 2:46
professionalRichard Andrew x6429-Apr-21 2:46 
AnswerRe: C# How to speed up for loop with large data iteration Pin
Richard MacCutchan29-Apr-21 3:07
mveRichard MacCutchan29-Apr-21 3:07 
AnswerRe: C# How to speed up for loop with large data iteration Pin
OriginalGriff29-Apr-21 4:16
mveOriginalGriff29-Apr-21 4:16 
AnswerRe: C# How to speed up for loop with large data iteration Pin
Gerry Schmitz29-Apr-21 9:03
mveGerry Schmitz29-Apr-21 9:03 
GeneralRe: C# How to speed up for loop with large data iteration Pin
jsc4229-Apr-21 22:29
professionaljsc4229-Apr-21 22:29 
GeneralRe: C# How to speed up for loop with large data iteration Pin
Gerry Schmitz30-Apr-21 8:22
mveGerry Schmitz30-Apr-21 8:22 
GeneralRe: C# How to speed up for loop with large data iteration Pin
jsc421-May-21 9:42
professionaljsc421-May-21 9:42 
QuestionHow to configure ad hoc network using C# windows form app for windows 10 Pin
Member 1517286227-Apr-21 4:09
Member 1517286227-Apr-21 4:09 
AnswerRe: How to configure ad hoc network using C# windows form app for windows 10 Pin
Gerry Schmitz27-Apr-21 8:02
mveGerry Schmitz27-Apr-21 8:02 
Questionsql server 2019 instances with C # Pin
Member 1407213825-Apr-21 20:50
Member 1407213825-Apr-21 20:50 
AnswerRe: sql server 2019 instances with C # Pin
OriginalGriff25-Apr-21 21:13
mveOriginalGriff25-Apr-21 21:13 
AnswerRe: sql server 2019 instances with C # Pin
Eddy Vluggen26-Apr-21 10:59
professionalEddy Vluggen26-Apr-21 10:59 
Questioncomputer science Pin
A.R ENTERTAIN24-Apr-21 15:17
A.R ENTERTAIN24-Apr-21 15:17 
AnswerRe: computer science Pin
Dave Kreskowiak24-Apr-21 15:42
mveDave Kreskowiak24-Apr-21 15:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.