C
Join ServerC#
help
await threads to finish their tasks [Answered]
VVeQox9/14/2022
I know this isnt really what threads should be used for, but ye...
, Basically im copying files mulitithreaded and want to await (doesnt need to be async await would be cool tho) so i can finish the task with some logs etc

BBinto869/14/2022
Im not sure why you dont use tasks?
SStroniax9/14/2022
Task.Run
abstracts this away to where you're effectively running a command on a separate thread, but don't need to explicitly manage the thread lifetime. If that is insufficient, you could also use TaskCompletionSource
and call SetCompleted
in a finally block in the body of the ThreadStart
delegate.VVeQox9/14/2022
does it have similar performance?
SStroniax9/14/2022
I believe tasks will usually be faster because they reuse existing threads in the ThreadPool in the normal situation.
SStroniax9/14/2022
I am not sure about
Task.Run
though, that might fire up a new thread.VVeQox9/14/2022
k will test tasks then 

Ccamel9/14/2022
Tasks run in the same thread afaik
Ccamel9/14/2022
Tasks are not multi threading
Ccamel9/14/2022
Itβs the order of execution that changes though
Mmtreit9/14/2022
This is not really true. The vast majority of tasks run on thread pool threads and are the basic mechanism for writings multi-threaded code to achieve parallelism in .net. What do you mean that tasks "run in the same thread" ?
Mmtreit9/14/2022
I mean, there is a reason Task is in the System.Threading namespace
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl
VVeQox9/14/2022
did some research and the answer is pretty obvious now (i think)
,
by using Thread.Join() on all of the threads the mainthread waits for all threads to finish
thx for the suggestions β€οΈ

by using Thread.Join() on all of the threads the mainthread waits for all threads to finish
thx for the suggestions β€οΈ
AAccord9/14/2022
β
This post has been marked as answered!
Mmtreit9/14/2022
I would not use Thread objects at all and use Task instead. There is almost no reason to use the Thread type instead of the Task type and Task has a much more ergonomic API.
VVeQox9/14/2022
gonna make two versions and compare them 

Jjcotton429/14/2022
@VeQox
Jjcotton429/14/2022
just do multiple async copies
Jjcotton429/14/2022
also see $nothread
VVeQox9/14/2022
Stream.CopyToAsync() ?
Jjcotton429/14/2022
yes
Mmtreit9/14/2022
How many files are you copying?
VVeQox9/14/2022
depends, wanted to play arround with making file copying faster (i know robocopy exists ...)
VVeQox9/14/2022
wanted to make a little project where i backup files from a folder to a different drive / nas, and y i could just use the default filesystem copy method i think so i can use the default windows ui for file copying, but what is the fun in making my life so easy π
Mmtreit9/14/2022
I kind of doubt you can beat robocopy in perf but give it a shot.
I pretty much never use the Windows UI for file copying because it's slow AF
I pretty much never use the Windows UI for file copying because it's slow AF
VVeQox9/14/2022
y, with the threadmethod i got similar results to robocopy maybe 1-2 seconds off of a 24gb 1000 files testset
Mmtreit9/14/2022
I'm just saying you're unlikely to make something dramatically faster. Ultimately you're going to probably hit I/O bottlenecks.
VVeQox9/14/2022
probably my network speed is gonna be the mainbottleneck when i want to backup files to a nas
VVeQox9/14/2022
but y, i would doubt that i make a faster way to copy files multithreaded since there aint allota options π
Jjcotton429/14/2022
rsync on Windows when
Ccamel9/15/2022
What I meant is that you need to let your new task run in a new thread, if you want it to be multithreaded.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/task-asynchronous-programming-model#BKMK_Threads
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/task-asynchronous-programming-model#BKMK_Threads
Ccamel9/15/2022
It's mostly about the non-blocking properties, not about parallelism. That is, if you don't start a new thread.
Mmtreit9/15/2022
The vast majority of "new tasks" will run on their own thread if they are doing anything cpu bound
Ccamel9/15/2022
ok, my bad