Task EventHandlers – The Little Secret You Didn’t Know
If you're writing asynchronous code in C# and using EventHandlers, odds are you've had issues with exceptions. Task EventHandlers might be consideration!
If you're writing asynchronous code in C# and using EventHandlers, odds are you've had issues with exceptions. Task EventHandlers might be consideration!
Dealing with async EventHandlers in C# can be very problematic. async void is a pattern cause headaches with exceptions. Check out this simple solution!
Background If you've poked through my previous postings, you'll probably notice that I love using events when I program. If I can find a reason to use an event, I probably will. I think they're a great tool that can really help you with designing your architectures, but there are certainly some common problems people run into when they use events. The one I want to address today has to do with memory leaks. That's right. I said it. Memory leaks in your .NET application. Just because it's a managed language doesn't mean your code can't be leaking memory! And now that I've got your attention, let's see how events might be causing some leakage in your application. (There is source that you can download and run. Check the summary section at the end!) Instance-Scope Event Handlers One of the…
Background Previously I wrote a bit about singletons. I'm not afraid to say that I think singletons have a time and a place... Okay, I'm afraid to say it, but it's actually how I feel :) After learning more and more about inversion of control design patterns and programming by interfaces, I started to notice just how inflexible singletons are. When you consider using a singleton, you should be considering both the pros and cons without jumping right into it. Here's an example of my approach for mixing singletons, programming by interfaces, and a bit of inversion of control. The Setup I'm actually surprised you got this far. I'm sure you're probably just sticking around to see how messed up this could possibly be. I'm actually proud that this little pattern has worked out so well when I've used it,…