Skip to content

Instantly share code, notes, and snippets.

@joshrobb
Forked from anonymous/BackgroundTasks.cs
Created April 12, 2011 11:56
Show Gist options
  • Save joshrobb/915378 to your computer and use it in GitHub Desktop.
Save joshrobb/915378 to your computer and use it in GitHub Desktop.
/*
Subject / EventLoopScheduler / CompositeDisposable are all from the Reactive Library for .Net 3.5.
Most of these classes are baked into .NET 4 AFAIK
*/
// Event loop and queue setup done in constructor
_sendQueue = new Subject<Action>();
var scheduler = new EventLoopScheduler( "EmailSender" );
var queueSubscription = _sendQueue.ObserveOn( scheduler ).Subscribe( action => action() );
_queueCleanup = new CompositeDisposable( queueSubscription, scheduler );
// Queueing a new task - just dumps an action on the queue
var sendDetails = new SendDetails {SurveyId = survey.Id, SenderId = sender.Id};
_sendQueue.OnNext( ()=> SendInvites( sendDetails ) );
// Cleanup the queue - encapsulating type implements IDisposable
_queueCleanup.Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment