C
C#11mo ago
matcfs

Progress bar and an TFTP library

Hi everyone, in my old .Net 4.7 Framework app I have a frame to performe a firmware upgrade thanks to a tftp library. The issue is about UI: until now I never succed to show the progress on my form because when I tried I I have always run into infamous Cross-thread operation not valid error. The library (https://github.com/Callisto82/tftp.net) that I use has a progress method that work very well in console. What do you think is the best solution to solve this? Something like BackgroundWorker/Thread for the TFTP library calls or something else? Thanks in advance for your help and sorry for this silly question.
GitHub
GitHub - Callisto82/tftp.net: Implements the TFTP (Trivial File Tra...
Implements the TFTP (Trivial File Transfer) protocol (client/server) in an easy-to-use C#/.NET library. - GitHub - Callisto82/tftp.net: Implements the TFTP (Trivial File Transfer) protocol (client...
6 Replies
canton7
canton711mo ago
You want Progress<T> / IProgress<T> I suspect. That handles the cross-thread dispatch for you
IProgress<TftpTransferProgress> progress = new Progress<TftpTransferProgress>(x => /* update some UI control with 'x' */);
client.Progress += (o, e) => progress.Report(e);
...
IProgress<TftpTransferProgress> progress = new Progress<TftpTransferProgress>(x => /* update some UI control with 'x' */);
client.Progress += (o, e) => progress.Report(e);
...
matcfs
matcfsOP11mo ago
Wow, thanks a lot for your suggestion. I'll try it asap ^^
canton7
canton711mo ago
(The normal pattern is that the thing that takes a while would accept an IProgress<T>... But if it just exposes an event that's easy enough to work around)
jcotton42
jcotton4211mo ago
I noticed you mentioned backgroundworker @matcfs, that shouldn't be used these days
jcotton42
jcotton4211mo ago
Task.Run vs BackgroundWorker: Intro
This is an introductory post for a new series that I’ll be doing comparing BackgroundWorker to Task.Run (in an async style). I always recommend Task.Run, and I have already written a long post describing why, but I still see some developers resisting the New Way of Doing Things (TM). So this will be a short series where I’ll compare the code sid...
matcfs
matcfsOP11mo ago
Ouch, I didn't know that, I'm such a newbie 🤦🏻‍♂️. This is a very useful thing to know, thx
Want results from more Discord servers?
Add your server