C
C#15mo ago
Meistro

❔ multithreading with Tasks

I have a question about C# multithreading or async methods. I'm currently trying to upload multiple files to a server via an API (with login data, etc.). My problem is that while waiting for all the tasks to complete, I want to query how many tasks have already finished, as long as the tasks are not yet completed. Specifically, I am uploading 30k files. Each file is started as a task. While the tasks are running, a loop should run during the await (await Task.WhenAll(uploadTasks);) to check how many tasks have already completed, and every 10k files, there should be a message like "Hey, I have uploaded 10k files and it's still running."
//<--- there is a code for the task above that runs all task

await Task.WhenAll(uploadTasks);

//decides how often should the system give a feedback every 10000 Files
runnerctr = 0;

//10.000 steps
int runner = 0;

// Loop that checks out hwo is process doing
while (runner < int.Parse(File_Upload_Count_Simu.Text))
{
//wait for some time before updating
await Task.Delay(TimeSpan.FromSeconds(5));

// loop through all tasks and update the counter for all done uploads(tasks)
foreach (var task in uploadTasks)
{
if (task.IsCompleted)
{
runner++;
}
}
// get progress every 10000 tasks which are done
if (runner >= runnerctr + 10000)
{
runnerctr += 10000;
UpdateRichTextBox("do something");
}
}
//<--- there is a code for the task above that runs all task

await Task.WhenAll(uploadTasks);

//decides how often should the system give a feedback every 10000 Files
runnerctr = 0;

//10.000 steps
int runner = 0;

// Loop that checks out hwo is process doing
while (runner < int.Parse(File_Upload_Count_Simu.Text))
{
//wait for some time before updating
await Task.Delay(TimeSpan.FromSeconds(5));

// loop through all tasks and update the counter for all done uploads(tasks)
foreach (var task in uploadTasks)
{
if (task.IsCompleted)
{
runner++;
}
}
// get progress every 10000 tasks which are done
if (runner >= runnerctr + 10000)
{
runnerctr += 10000;
UpdateRichTextBox("do something");
}
}
it is working by the way ...but I am not sure whether it is realy counting while awating or not.
77 Replies
sibber
sibber15mo ago
async await isnt multithreading btw anyway youre waiting for all the tasks to finish, then running the loop
Meistro
Meistro15mo ago
Oh, thank you for correction 🙏🏽
sibber
sibber15mo ago
in other words, the loop wont run until all the tasks have finished so youre not counting anything
Meistro
Meistro15mo ago
Is there a way to do the loop while waiting ?
sibber
sibber15mo ago
await after the loop
DaVinki
DaVinki15mo ago
Out of curiosity are you uploading each file individually
Meistro
Meistro15mo ago
Sounds legit 😅 Every file is a task and they are all starting within a while loop. After the while loop the code above runs
sibber
sibber15mo ago
also this could be simplified
Meistro
Meistro15mo ago
The file upload is a HTTP POST request btw. And the task is done when the http gives a ok back I really want to be open to any idea. I'm still a beginner and need your help
sibber
sibber15mo ago
you want to send a message every 10k tasks right?
Meistro
Meistro15mo ago
Maybe I missunderstood your question Yes right
sibber
sibber15mo ago
you probably dont need anything more complicated than this then:
Task aggregateTask = Task.WhenAll(uploadTasks);

int lastCompletedTaskCount = 0;
while (!aggregateTask.IsCompleted)
{
await Task.Delay(TimeSpan.FromSeconds(5));

int completedTaskCount = uploadTasks.Count(x => x.IsCompleted);
if (completedTaskCount - lastCompletedTaskCount >= 10_000)
{
SendMessage();
lastCompletedTaskCount = (completedTaskCount / 10_000) * 10_000;
}
}

await aggregateTask;
Task aggregateTask = Task.WhenAll(uploadTasks);

int lastCompletedTaskCount = 0;
while (!aggregateTask.IsCompleted)
{
await Task.Delay(TimeSpan.FromSeconds(5));

int completedTaskCount = uploadTasks.Count(x => x.IsCompleted);
if (completedTaskCount - lastCompletedTaskCount >= 10_000)
{
SendMessage();
lastCompletedTaskCount = (completedTaskCount / 10_000) * 10_000;
}
}

await aggregateTask;
like we said, await after the loop
Meistro
Meistro15mo ago
Oh wow !!! Thank you very much gor the effort !!! 🙏🏽 This looks interessting xD but will it give a message every 10k file ? Example I want to upload 50k files I need the programm to give me every 10k a message (how long did it took in everage etc.)
DaVinki
DaVinki15mo ago
I never realized Task.WhenAll is a task itself, that's useful lol
Meistro
Meistro15mo ago
But ! I can work with this ! Thank you so much !
sibber
sibber15mo ago
sure np
Meistro
Meistro15mo ago
How usefull ? 😅
sibber
sibber15mo ago
there was a small bug, fixed it it should but you should test it i havent
Meistro
Meistro15mo ago
Thank you … ah btw. Do I need the Task.delay(….? Yes I will
sibber
sibber15mo ago
well not really, but you probably should have some delay you had a delay of 5 secs in your original code, so i kept it
Meistro
Meistro15mo ago
its for my student Job you know… if I dont finish the project .. they might kick me from company Ah ok thank you :))) happy you answered so fast !!!! Great job !!!!
sibber
sibber15mo ago
actually theres another bug lol
Meistro
Meistro15mo ago
which one ?
sibber
sibber15mo ago
alright fixed it before, it wouldnt exactly send a message every 10k because imagine the first iteration it did 10,001
Meistro
Meistro15mo ago
yes
sibber
sibber15mo ago
that means the second would need to at have 20,001 tasks done for it to send a message not 20,000
DaVinki
DaVinki15mo ago
Instead of spinning, what if you used a countdown event?
sibber
sibber15mo ago
i.e. it used to send a message when 10k tasks were done since the last iteration, not every 10k you could probably clean this up a bit
Meistro
Meistro15mo ago
hmmm
sibber
sibber15mo ago
but it works should work
Meistro
Meistro15mo ago
you tryed it ?
sibber
sibber15mo ago
wdym no i tried it in my head which isnt reliable
Meistro
Meistro15mo ago
well dumb question 😄
sibber
sibber15mo ago
no, good question
DaVinki
DaVinki15mo ago
Remove the while loop and replace it with a blocking CountdownEvent with an initial count of 10,000, decremented at the end of every successful task
sibber
sibber15mo ago
i shouldve said "it should work"
Meistro
Meistro15mo ago
I am confused 😄 I am a beginner I can show you the whole method guys .. What I have right now if you want
sibber
sibber15mo ago
oh TIL this exists
Meistro
Meistro15mo ago
should I use this countdown event ? My main goal is that the task counting should be live I mean not after when all tasks are done but while the tasks arent still done you know what I mean ? Like when I download something... it shows something like 10% of 100% my problem before was, it showed me 10% of 100% done but actualy its done for long time haha sorry for my bad english
sibber
sibber15mo ago
actually thats not exactly what we need here this signals when it reaches zero, we dont need that it would also require each task to have a reference to the cde
DaVinki
DaVinki15mo ago
But you can get rid of Task.Delay and the spinning
sibber
sibber15mo ago
not really? how does this replace that
Meistro
Meistro15mo ago
this is the method guys 😄
DaVinki
DaVinki15mo ago
It's blocking so you can wait it three times Preferably in a for loop in case maybe there are 20k or 40k requests
Meistro
Meistro15mo ago
there are some german comments in it.. sorry for that you mean I should use for loop instead of while ?
DaVinki
DaVinki15mo ago
For a beginner you should probably stick with the while version
Meistro
Meistro15mo ago
ok ok
Florian Voß
Florian Voß15mo ago
@Meistro is it normal in your company to write the code in english but the comments in german? Cuz I'm german too, we have projects where we write both in german or both in english but that is incosistent what you have
Meistro
Meistro15mo ago
hei Florian 😄 no its not normal tho... I didnt push the code to Git yet. But I will change all comments into english as soon as I have a stable version 😄 sometimes its in german becauz I dint have time to translate it and I wanted a quick note for myself 😄
Florian Voß
Florian Voß15mo ago
sure, just make sure you dont forget adjusting that before pushing
Meistro
Meistro15mo ago
Yes I will! thanks for the advice 🙂 did this helped a bit ? or is it too much ? I know its confusing but I thought it might clarify things 😄 I think it worked
Meistro
Meistro15mo ago
i mean "72,09682680000013 seconds" is not correct but I will fix this anyways
sibber
sibber15mo ago
so it worked?
Meistro
Meistro15mo ago
yeah ! 😄 realy Happy thank you !
sibber
sibber15mo ago
nice np :)
Meistro
Meistro15mo ago
this dumb method took me a whole day and I did not fix anything alone 😄 realy ! thank you very much !!!!
sibber
sibber15mo ago
happy to help
Meistro
Meistro15mo ago
🙂 is it ok if I delete the code I posted above ?
sibber
sibber15mo ago
ofc
Meistro
Meistro15mo ago
ok thx
sibber
sibber15mo ago
$close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Meistro
Meistro15mo ago
thanks again and have a good week !
sibber
sibber15mo ago
you too :)
Meistro
Meistro15mo ago
thx thx $close
MODiX
MODiX15mo ago
Use the /close command to mark a forum thread as answered
Meistro
Meistro15mo ago
the close doesnt work 😄 I tryed /close but it doesnt work xD
sibber
sibber15mo ago
oh looks like the bot is down oops
Meistro
Meistro15mo ago
another/next mission for you ! 😛
sibber
sibber15mo ago
its fine just leave it lol its not our bot
Meistro
Meistro15mo ago
ah kk
DaVinki
DaVinki15mo ago
@Cyberrex I think we may have been overcomplicating it, you could just go through every task and ContinueWith a check on an atomic integer or similar state object and run a function dependent on the state, then just Task.WaitAll
sibber
sibber15mo ago
that would be overcomplicating it and would also be a lot more inefficient especially when you have tens of thousands of files
DaVinki
DaVinki15mo ago
It would be simpler but sure, less efficient
Jester
Jester15mo ago
im just wondering if its fine to have 50k tasks
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.