patterncsharpMinor
Handling events in a multi threaded environment
Viewed 0 times
handlingmultieventsenvironmentthreaded
Problem
I have a service that raises multiple events, some of them can be raised at the same time. I need to process those events and run a potentially long running method based on the event arguments.
What I did is to create a
This is my code:
```
public class EventsTest
{
//private fields
private BlockingCollection _queue; //a concurrent collection to hold incoming events
private CancellationTokenSource _tokenSource;
private IoService _ioService; //a service that raises multiple events
private static EventWaitHandle _eventWaiter; // used for waiting inside a method
public EventsTest()
{
_queue = new BlockingCollection();
_tokenSource = new CancellationTokenSource();
_eventWaiter = new EventWaitHandle(false, EventResetMode.AutoReset);
_ioService = new IoService();
_ioService.IoEvent += _ioService_IoEvent;
//Start Listening to IO events
var t = Task.Factory.StartNew(StartListening, _tokenSource, TaskCreationOptions.LongRunning);
}
//IO events listener
private void _ioService_IoEvent(byte[] desc, int portNum)
{
//add events to a blocking collection
_queue.Add(portNum);
}
//a while loop running on a separate task
//and taking the event one by one
private void StartListening(object dummy)
{
while (!_tokenSource.IsCancellationRequested)
{
try
{
var eve = _queue.Take(_tokenSource.Token);
switch (eve)
{
case 0:
//run the required method on a separate task so it won't block the loop
Task.Factory.StartNew(LongRunningMethod);
break;
case 1:
//invoke a po
What I did is to create a
BlockingCollection that will store the events and a Task that will keep taking one event at a time until it will be signaled to stop using a CancellationTokenSource.This is my code:
```
public class EventsTest
{
//private fields
private BlockingCollection _queue; //a concurrent collection to hold incoming events
private CancellationTokenSource _tokenSource;
private IoService _ioService; //a service that raises multiple events
private static EventWaitHandle _eventWaiter; // used for waiting inside a method
public EventsTest()
{
_queue = new BlockingCollection();
_tokenSource = new CancellationTokenSource();
_eventWaiter = new EventWaitHandle(false, EventResetMode.AutoReset);
_ioService = new IoService();
_ioService.IoEvent += _ioService_IoEvent;
//Start Listening to IO events
var t = Task.Factory.StartNew(StartListening, _tokenSource, TaskCreationOptions.LongRunning);
}
//IO events listener
private void _ioService_IoEvent(byte[] desc, int portNum)
{
//add events to a blocking collection
_queue.Add(portNum);
}
//a while loop running on a separate task
//and taking the event one by one
private void StartListening(object dummy)
{
while (!_tokenSource.IsCancellationRequested)
{
try
{
var eve = _queue.Take(_tokenSource.Token);
switch (eve)
{
case 0:
//run the required method on a separate task so it won't block the loop
Task.Factory.StartNew(LongRunningMethod);
break;
case 1:
//invoke a po
Solution
-
The method
-
The field
The method
StartListening takes one argument object dummy. It is not used in the method and from its name I conclude that it is not intended to be used. It is possible to declare the method with no arguments and call a variant of Task.Factory.StartNew that takes a parameter of type Action and TaskCreationOptions to start the new task.-
The field
private static EventWaitHandle _eventWaiter is only initialized in the constructor and never used. Its purpose is unclear from the code.Context
StackExchange Code Review Q#115508, answer score: 3
Revisions (0)
No revisions yet.