Anonymous Methods
I just came across another situation where anonymous methods would be really useful: when using BeginInvoke (and presumably Invoke, as well).
Now I realise that this may not be the best design, but I have an app which imports a bunch of files into a database. To keep the GUI responsive, I've moved all the file loading/parsing/storing stuff into a separate thread. But to keep the user informed, this thread needs to update the gui with the filename it's currently processing, any errors that it comes across in the file, and it keeps a progress bar updated as well. So basically it's a non-gui thread that needs to update the gui updated.
Now I know that it's not a good thing to update the gui from a non-gui thread (although it seemed to work fine, but that's just chance, I guess), so I need to use BeginInvoke to update the gui in the context of the main, gui, thread instead. And that's fine, but a lot of the updates are just one-liners to set labels on the form, and it seems a bit painful to declare a delegate and write a method just to be able to call one line via BeginInvoke.
And this is where anonymous methods would be really useful. I suspect that they will be often abused, and they can dramatically reduce code readability, but this seems like the textbook situation for their use.
Labels: Coding
0 Comments:
Post a Comment
<< Home