yield return out of a thread
Is it possible to yield return a value out of a thread, ik a thread only takes void functions, but with () => i can avoid that.
24 Replies
how do you expect this to work
like hwat are you trying to accomplish
well
I think you might be looking for https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-6.0
ConcurrentQueue Class (System.Collections.Concurrent)
Represents a thread-safe first in-first out (FIFO) collection.
you can push elements onto it from one thread and consume them from another
lets not jump to solutioning until we know what the actual issue is
alright, im copying files and want kind of live feedback of what latest file is that has been copied, with sync thats pretty straight forward, and i kinda dont want to output in the thread itself bc i would need a mutex for that and that slows it down bc its gonna get spammed.
so i got a enumerable<FileInfo> or smth like that and i loop over that and always get the latest file
then it sounds like you want a concurrent stack https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentstack-1?view=net-6.0
ConcurrentStack Class (System.Collections.Concurrent)
Represents a thread-safe last in-first out (LIFO) collection.
top of stack is the latest copied file
so do i make a kind of busy waiting checking if the latest changed outside or how would i approach this
look into the IProgress<> interface
like for a UI
?
we have no idea hwat ur UI needs are
a pattern is to pass that into an
async
method or Task.Run and use it to get progress reports on when stuff finishes/how far along a task isbasic progressbar
like basic progressbar where
is this a website? a cli? a winforms app... wpf?/?
basic console
a thread finished copying a file, returns it, i display the lates file and so on
if its console i guess why are you running stuff on a new thread?
make things faster
why would a thread make it faster
are you firing off multiple threads to download many files at once?
y
?
y what
yes
unless u meant yes
ahh
-_-
so we have multiple downloads at once, and you have a need to update a shared object when they finish
or you want each file to report its own progress
basically do you just care about which ones have finished or do you want total progress on downlaods in bytes
i only really care about the ones that finished
cool, so the concurrent stack will work
shared stack object, when a thread finishes it pushes the file name to the stack, im assuming you then have code in the main thread that is checking that stack for TryPeek
there are other ways oc, perhaps with Channel<T>
but concurrentStack works