Best Practices When Stopping and Starting a Filtered Stream In A Task

Sep 3, 2014 at 3:11 AM
I'm kicking off a filtered stream like this:
At some dynamic event, I need to stop the stream, make a change to the keywords and then restart the stream. However, I need to act on the stream that's running inside the task I kicked off and let the code keep running. This is running in an Azure worker role and I'm updating a heartbeat and checking for the special event in parallel.

If I call filteredStream.StopStream() directly outside the task, it works great. When the stop delegate is called, I do what I need to inside that block and then restart the streaming again.

But, because the StopStream() method never returns from starting the stream again, the code just sits there at the call to StopStream().

If I kick off another task like this, it works:
However, I'm afraid that this will result in a resource leak because I'm kicking off another task every time the event is detected.

Does anyone have a best practice recommendation for acting on the stream that is started using Task.Factory.StartNew()?