Skip to content

Instantly share code, notes, and snippets.

@MikeBild
Created November 11, 2012 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeBild/4055567 to your computer and use it in GitHub Desktop.
Save MikeBild/4055567 to your computer and use it in GitHub Desktop.
Shared resource management with Rx
public class SharedResource : IDisposable
{
public int SequenceNumber { get; private set; }
public SharedResource()
{
Debug.WriteLine("SharedResource created.");
}
public void Increment()
{
SequenceNumber++;
Debug.WriteLine(SequenceNumber);
}
public void Dispose()
{
SequenceNumber = 0;
Debug.WriteLine("SharedResource completed.");
}
}
class Program
{
static void Main(string[] args)
{
var underlaying = Observable
.Interval(TimeSpan.FromSeconds(1))
.Take(10)
.Do(_ => Debug.WriteLine("Tick: {0}", _), () => Debug.WriteLine("Ticks done"));
var stream = Observable
.Using(() => new SharedResource(), resource => underlaying.Do(l => resource.Increment()))
.Publish()
.RefCount(); ;
var first = stream.Subscribe(_ => Debug.WriteLine("First: {0}", _), () => Debug.WriteLine("first done"));
var second = stream.Subscribe(_ => Debug.WriteLine("Second: {0}", _), () => Debug.WriteLine("second done"));
Console.ReadLine();
first.Dispose();
Console.ReadLine();
second.Dispose();
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment