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