await threads to finish their tasks [Answered]
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
27 Replies
Im not sure why you dont use tasks?
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.does it have similar performance?
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.k will test tasks then
Tasks run in the same thread afaik
Tasks are not multi threading
Itβs the order of execution that changes though
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" ?
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.
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 β€οΈ
β
This post has been marked as answered!
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.
gonna make two versions and compare them
@VeQox
just do multiple async copies
also see $nothread
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
Stream.CopyToAsync() ?
yes
How many files are you copying?
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 π
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
y, with the threadmethod i got similar results to robocopy maybe 1-2 seconds off of a 24gb 1000 files testset
I'm just saying you're unlikely to make something dramatically faster. Ultimately you're going to probably hit I/O bottlenecks.
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 π
rsync on Windows when
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#.
It's mostly about the non-blocking properties, not about parallelism. That is, if you don't start a new thread.
The vast majority of "new tasks" will run on their own thread if they are doing anything cpu bound
ok, my bad