C
C#

help

await threads to finish their tasks [Answered]

VVeQox9/14/2022
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
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. I am not sure about Task.Run though, that might fire up a new thread.
VVeQox9/14/2022
k will test tasks then HmmNoted
Ccamel9/14/2022
Tasks run in the same thread afaik Tasks are not multi threading 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
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.
VVeQox9/14/2022
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 ❀️
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 peepoProgramming
Jjcotton429/14/2022
@VeQox just do multiple async copies also see $nothread
MMODiX9/14/2022
There Is No Thread
This is an essential truth of async in its purest form: There is no thread.
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 ...) 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
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 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
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#.
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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
vs nullHi I have a test where: Assert.AreEqual(null, object.attribute) In the test the attribute is not inI want to pay someone to do a project for me for schoolHello everyone. I am a student in the UK and I am doing computer science. Part of my course requiremManipulate browser session storage manually to hack claims into BlazorI'm trying to figure out how to manually add a claim through the browser so that Blazor shows me thiTesting a class that relies on timersI have a class `RoutineManager` that uses another class `TimerScheduler` to schedule different actioWTH is going in this code?var ip = new double[] { 1, 2, 3 }; double sum = 0; var output = ip.Select(w => sum += w); var l1 = Not avialible in CSharp 9.0 use 10.0 or greaterHey, can I just update it? Or do I have to reinstall C#. It's not so long ago I installed C#, It's w❔ Using the output cache in a web apiI have a method to get a pdf file and want to use output cache on it. The `[OutputCache]` attribute dependency injection in net frameworkhi all, i understand that in net core we can add dependency injection like this ``` IConfiguration CProcessing incoming networking messagesSay I have a processing loop which processes incoming network messages asynchronously with ConfigureReplying with RebusI'm trying to develop an application, where services communicates via Rebus, with RabbitMQ is transpVSCode working with SharedProjects [Answered]VSCode seems to have problems with adding files to .projitems when working with SharedProjects. Are 2D Scrolling Background in UnityHello fellow Programmers, I am a new programmer that is self-taught, would anyone be able to help meBlazor WASM Authentication with separate APIsI have a Blazor WASM app and an API for that client app. Now I'd like to decouple things and extractappsettings.json in legacy applicationhi all, is it possible to use appsettings.json as a configuration file instead of a web/app.config iDownload file, decompress and keep in memoryI have an endpoint that serves a gzipped .tsv file. I want to download the file, decompress it and wDotnetCLI Get Project Root Path - Database Recreation Script```ps Set-Location (get-item $PSScriptRoot).Parent.Parent.FullName dotnet ef database drop --contextHangman exercise - line 41-47 not behaving.``` https://pastebin.com/NdZFzCcX ```explicit null paramis there a way to specify an explicit overload for `null` ? eg foo(object_a); // object overload fUse X509Certificate2The screenshot is the default template generated by VS How do I add X509Certificate2 to it?How do i create a external file containing previously entered strings?My program (a gtin verifier) asks the user to enter a gtin, which it saves as a string. After that t