❔ 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."
it is working by the way ...but I am not sure whether it is realy counting while awating or not.
77 Replies
async await isnt multithreading btw
anyway
youre waiting for all the tasks to finish, then running the loop
Oh, thank you for correction 🙏🏽
in other words, the loop wont run until all the tasks have finished
so youre not counting anything
Is there a way to do the loop while waiting ?
await after the loop
Out of curiosity are you uploading each file individually
Sounds legit 😅
Every file is a task and they are all starting within a while loop. After the while loop the code above runs
also this could be simplified
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
you want to send a message every 10k tasks right?
Maybe I missunderstood your question
Yes right
you probably dont need anything more complicated than this then:
like we said, await after the loop
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.)
I never realized Task.WhenAll is a task itself, that's useful lol
But ! I can work with this ! Thank you so much !
sure np
How usefull ? 😅
there was a small bug, fixed it
it should
but you should test it
i havent
Thank you … ah btw. Do I need the Task.delay(….?
Yes I will
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
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 !!!!
actually theres another bug lol
which one ?
alright fixed it
before, it wouldnt exactly send a message every 10k
because imagine the first iteration it did 10,001
yes
that means the second would need to at have 20,001 tasks done
for it to send a message
not 20,000
Instead of spinning, what if you used a countdown event?
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
hmmm
but it works should work
you tryed it ?
wdym
no
i tried it in my head
which isnt reliable
well dumb question 😄
no, good question
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
i shouldve said "it should work"
I am confused 😄 I am a beginner
I can show you the whole method guys .. What I have right now
if you want
oh TIL this exists
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
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
But you can get rid of Task.Delay and the spinning
not really?
how does this replace that
this is the method guys 😄
It's blocking so you can wait it three times
Preferably in a for loop in case maybe there are 20k or 40k requests
there are some german comments in it.. sorry for that
you mean I should use for loop instead of while ?
For a beginner you should probably stick with the while version
ok ok
@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
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 😄
sure, just make sure you dont forget adjusting that before pushing
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
i mean "72,09682680000013 seconds" is not correct but I will fix this anyways
so it worked?
yeah ! 😄 realy Happy thank you !
nice
np :)
this dumb method took me a whole day and I did not fix anything alone 😄
realy ! thank you very much !!!!
happy to help
🙂
is it ok if I delete the code I posted above ?
ofc
ok thx
$close
Use the
/close
command to mark a forum thread as answeredthanks again and have a good week !
you too :)
thx thx
$close
Use the
/close
command to mark a forum thread as answeredthe close doesnt work 😄 I tryed /close but it doesnt work xD
oh looks like the bot is down
oops
another/next mission for you ! 😛
its fine just leave it
lol its not our bot
ah kk
@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
that would be overcomplicating it
and would also be a lot more inefficient
especially when you have tens of thousands of files
It would be simpler but sure, less efficient
im just wondering if its fine to have 50k tasks
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.