C
C#β€’2y ago
VeQox

await threads to finish their tasks [Answered]

I know this isnt really what threads should be used for, but ye... Hmm , 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
27 Replies
Binto86
Binto86β€’2y ago
Im not sure why you dont use tasks?
Stroniax
Stroniaxβ€’2y ago
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.
VeQox
VeQoxβ€’2y ago
does it have similar performance?
Stroniax
Stroniaxβ€’2y ago
I believe tasks will usually be faster because they reuse existing threads in the ThreadPool in the normal situation. I am not sure about Task.Run though, that might fire up a new thread.
VeQox
VeQoxβ€’2y ago
k will test tasks then HmmNoted
camel
camelβ€’2y ago
Tasks run in the same thread afaik Tasks are not multi threading It’s the order of execution that changes though
mtreit
mtreitβ€’2y ago
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" ?
mtreit
mtreitβ€’2y ago
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
Task Parallel Library (TPL)
Explore the Task Parallel Library (TPL), a set of public types and APIs to simplify the process of adding parallelism & concurrency to applications in .NET.
VeQox
VeQoxβ€’2y ago
did some research and the answer is pretty obvious now (i think) pachaperfect , by using Thread.Join() on all of the threads the mainthread waits for all threads to finish thx for the suggestions ❀️
Accord
Accordβ€’2y ago
βœ… This post has been marked as answered!
mtreit
mtreitβ€’2y ago
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.
VeQox
VeQoxβ€’2y ago
gonna make two versions and compare them peepoProgramming
jcotton42
jcotton42β€’2y ago
@VeQox just do multiple async copies also see $nothread
MODiX
MODiXβ€’2y ago
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
VeQox
VeQoxβ€’2y ago
Stream.CopyToAsync() ?
jcotton42
jcotton42β€’2y ago
yes
mtreit
mtreitβ€’2y ago
How many files are you copying?
VeQox
VeQoxβ€’2y ago
depends, wanted to play arround with making file copying faster (i know robocopy exists ...) 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 πŸ˜‰
mtreit
mtreitβ€’2y ago
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
VeQox
VeQoxβ€’2y ago
y, with the threadmethod i got similar results to robocopy maybe 1-2 seconds off of a 24gb 1000 files testset
mtreit
mtreitβ€’2y ago
I'm just saying you're unlikely to make something dramatically faster. Ultimately you're going to probably hit I/O bottlenecks.
VeQox
VeQoxβ€’2y ago
probably my network speed is gonna be the mainbottleneck when i want to backup files to a nas but y, i would doubt that i make a faster way to copy files multithreaded since there aint allota options πŸ™‚
jcotton42
jcotton42β€’2y ago
rsync on Windows when
camel
camelβ€’2y ago
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
The Task Asynchronous Programming (TAP) model with async and await ...
Learn when and how to use Task-based async programming, a simplified approach to asynchronous programming in C#.
camel
camelβ€’2y ago
It's mostly about the non-blocking properties, not about parallelism. That is, if you don't start a new thread.
mtreit
mtreitβ€’2y ago
The vast majority of "new tasks" will run on their own thread if they are doing anything cpu bound
camel
camelβ€’2y ago
ok, my bad