Event-driven .NET: How to unit test code that depends on an event

Testing code with a dependency on an event can be tricky. You may be tempted to directly unit test the event handling method. However, that doesn’t prove that code under test is listening for the event. It’s better to really fire the event, and then test side effects of the event handling method. Firing the … Read more

Event-driven .NET: Concurrent Producer/Consumer using a non-blocking, async queue

In a previous article I wrote about how to implement concurrent producer/consumer using a BlockingCollection. This is a thread-safe, event-driven approach that uses high-level concurrent constructs. The only downside is that the consumer uses a blocking call to dequeue messages. In other words, it wastes a thread. Is there a way to implement this using … Read more

Event-driven .NET: use FileSystemWatcher instead of polling for new files

You can use FileSystemWatcher to get notified of file system changes, such as when a file is created. Here’s an example of reacting to when a JSON file is created: Compared to polling, this event-driven approach with FileSystemWatcher is more efficient and simpler. In the remainder of this article I’ll show an example of how … Read more

Event-driven .NET: Concurrent Producer/Consumer using BlockingCollection

With the Producer/Consumer pattern you have one or more threads producing new work and enqueuing it, and one or more threads consuming that work by dequeuing it and processing it. The consumers and producers share access to the work queue. Think of it like the Post Office. You have one or more people (producers) dropping … Read more

Event-driven .NET: How to use query notifications in SQL Server to monitor database changes

How do you check for new records in a database and react to the new data? You really only have two options: Poll for changes every X seconds Use query notifications to be notified when new data is inserted In this article I’ll show you how to configure query notifications so that your application receives … Read more

Event-driven .NET: How to add an event to a class

In the previous article we saw how events work. In this article I’ll show how to add an event to a class. In order to illustrate this I’ve created a simple chat app that uses an in-memory message queue. Each chat window is subscribed to the message queue’s OnMessageReceived event, and updates their UI upon … Read more