C
C#3y ago
VeQox

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
Jayy
Jayy3y ago
how do you expect this to work like hwat are you trying to accomplish
VeQox
VeQoxOP3y ago
well
Aaron
Aaron3y ago
you can push elements onto it from one thread and consume them from another
Jayy
Jayy3y ago
lets not jump to solutioning until we know what the actual issue is
VeQox
VeQoxOP3y ago
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
Jayy
Jayy3y ago
ConcurrentStack Class (System.Collections.Concurrent)
Represents a thread-safe last in-first out (LIFO) collection.
Jayy
Jayy3y ago
top of stack is the latest copied file
VeQox
VeQoxOP3y ago
so do i make a kind of busy waiting checking if the latest changed outside or how would i approach this
Becquerel
Becquerel3y ago
look into the IProgress<> interface
Jayy
Jayy3y ago
like for a UI ? we have no idea hwat ur UI needs are
Becquerel
Becquerel3y ago
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 is
VeQox
VeQoxOP3y ago
basic progressbar
Jayy
Jayy3y ago
like basic progressbar where is this a website? a cli? a winforms app... wpf?/?
VeQox
VeQoxOP3y ago
basic console a thread finished copying a file, returns it, i display the lates file and so on
Jayy
Jayy3y ago
if its console i guess why are you running stuff on a new thread?
VeQox
VeQoxOP3y ago
make things faster Hmm
Jayy
Jayy3y ago
why would a thread make it faster are you firing off multiple threads to download many files at once?
VeQox
VeQoxOP3y ago
y
Jayy
Jayy3y ago
? y what
VeQox
VeQoxOP3y ago
yes
Jayy
Jayy3y ago
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
VeQox
VeQoxOP3y ago
i only really care about the ones that finished
Jayy
Jayy3y ago
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
Want results from more Discord servers?
Add your server