本文整理汇总了C#中CompositeDisposable类的典型用法代码示例。如果您正苦于以下问题:C# CompositeDisposable类的具体用法?C# CompositeDisposable怎么用?C# CompositeDisposable使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
CompositeDisposable类属于命名空间,在下文中一共展示了CompositeDisposable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HandleClients
public static IDisposable HandleClients(TcpListener listener, Func<TcpClient, string, Task> handleClientAccessPolicy, string policyResponse)
{
listener.Start();
var disposableHandler = ThreadPoolScheduler.Instance.ScheduleAsync(async (scheduler, ct) =>
{
var disposable = new BooleanDisposable();
while (!disposable.IsDisposed)
{
var client = await listener.AcceptTcpClientAsync();
await handleClientAccessPolicy(client, policyResponse);
await scheduler.Yield();
}
return disposable;
});
var compositeDisposable = new CompositeDisposable();
compositeDisposable.Add(Disposable.Create(() => listener.Stop()));
compositeDisposable.Add(disposableHandler);
return compositeDisposable;
}
示例2: Receive
public IObservable<string> Receive(byte[] buffer, IScheduler scheduler)
{
return Observable.Create<string>(observer =>
{
var disposable = new CompositeDisposable();
this.connectionToken.SocketEvent.SetBuffer(buffer, 0, buffer.Length);
var subject = new Subject<Unit>();
var disposableEventSubscription = connectionToken.SocketEvent.Completed.Subscribe(_ =>
{
if (SendNotificationToObserver(observer, connectionToken.SocketEvent))
{
subject.OnNext(Unit.Default);
}
});
var disposableActions = subject.ObserveOn(scheduler).Subscribe(_ =>
{
if (!connectionToken.Socket.ReceiveAsync(connectionToken.SocketEvent))
{
if (SendNotificationToObserver(observer, connectionToken.SocketEvent))
{
subject.OnNext(Unit.Default);
}
}
});
subject.OnNext(Unit.Default);
disposable.Add(disposableEventSubscription);
disposable.Add(disposableActions);
return disposable;
});
}
示例3: Initialize
internal static void Initialize()
{
if (!Setting.IsLoaded || !Setting.IsBehaviorLogEnabled.Value)
{
return;
}
var disposables = new CompositeDisposable();
try
{
var file = new FileStream(
Path.Combine(App.ConfigurationDirectoryPath, App.BehaviorLogFileName),
FileMode.Append,
FileAccess.Write,
FileShare.ReadWrite);
var writer = new StreamWriter(file) { AutoFlush = true };
_writer = writer.WriteLine;
disposables.Add(Disposable.Create(() => _writer = null));
disposables.Add(writer);
disposables.Add(file);
}
finally
{
_disposable = disposables;
}
}
示例4: PrimeExperiment
public void PrimeExperiment()
{
IObservable<long> xs = Observable.Interval(TimeSpan.FromSeconds(1))
.Do(ConsoleOutput(Text.Generated))
.Take(3);
IObservable<long> pruned = xs.PublishLast().Prime();
for (int r = 0; r < 2; r++)
{
using (var subscriptions = new CompositeDisposable())
{
for (int s = 0; s < 5; s++)
{
Thread.Sleep(TimeSpan.FromSeconds(.4));
TraceLine(Text.SubscribingFormat, s);
subscriptions.Add(
pruned.Subscribe(ConsoleOutput(Text.NamedObserverFormat, s)));
}
Thread.Sleep(TimeSpan.FromSeconds(2));
}
}
}
示例5: AutoDisposeSampleProvider
public AutoDisposeSampleProvider(ISampleProvider provider,
IEnumerable<IDisposable> disposables)
{
this._provider = provider;
this._disposables = new CompositeDisposable(disposables);
this.WaveFormat = provider.WaveFormat;
}
示例6: ReactiveListener
/// <summary>
/// Initializes the listener with the given port.
/// </summary>
public ReactiveListener(int port)
{
this.port = port;
tracer.ReactiveListenerCreated(port);
this.socketDisposable = new CompositeDisposable();
}
示例7: PressableController
public PressableController(FrameworkElement element, PressableModel model, KinectRegion kinectRegion)
{
this.element = new WeakReference(element);
this.kinectRegion = kinectRegion;
this.inputModel = model;
if (this.inputModel == null)
return;
this.eventSubscriptions = new CompositeDisposable
{
this.inputModel.PressingStartedObservable()
.Subscribe(_ => VisualStateManager.GoToState(this.Control, "Focused", true)),
this.inputModel.HoldingObservable()
.Subscribe(_ => Debug.WriteLine(string.Format("Holding: {0}, ", DateTime.Now))),
this.inputModel.PressingUpdatedObservable()
.Subscribe(_ => Debug.WriteLine(string.Format("PressingUpdated: {0}, ", DateTime.Now))),
this.inputModel.PressingCompletedObservable()
.Subscribe(_ => VisualStateManager.GoToState(this.Control, "Unfocused", true)),
this.inputModel.TappedObservable()
.Subscribe(_ => Debug.WriteLine(string.Format("Tapped: {0}, ", DateTime.Now))),
};
}
示例8: Clear
public void Clear()
{
var d = new CompositeDisposable (Disposable.Empty);
Assert.AreEqual (1, d.Count, "#1");
d.Clear ();
Assert.AreEqual (0, d.Count, "#2");
}
示例9: ExecutionContext
public ExecutionContext(TimeSpan skipAhead = default(TimeSpan))
{
this.disposables = new CompositeDisposable();
this.cancelRequested = new BehaviorSubject<bool>(false)
.AddTo(this.disposables);
this.progressDeltas = new Subject<TimeSpan>()
.AddTo(this.disposables);
this
.cancelRequested
.Where(x => x)
.Subscribe(_ => this.IsCancelled = true)
.AddTo(this.disposables);
this
.progressDeltas
.Scan((running, next) => running + next)
.Subscribe(x => this.Progress = x)
.AddTo(this.disposables);
this
.WhenAnyValue(x => x.CurrentExercise)
.Select(x => this.progressDeltas.StartWith(TimeSpan.Zero).Scan((running, next) => running + next))
.Switch()
.Subscribe(x => this.CurrentExerciseProgress = x)
.AddTo(this.disposables);
this
.progressDeltas
.StartWith(skipAhead)
.Scan((running, next) => running - next)
.Select(x => x < TimeSpan.Zero ? TimeSpan.Zero : x)
.Subscribe(x => this.SkipAhead = x)
.AddTo(this.disposables);
}
示例10: PlaySound
public static void PlaySound(string fileName)
{
var disposables = new CompositeDisposable();
var sampleProvider = GetSampleProvider(fileName, disposables);
var wrapper = new AutoDisposeSampleProvider(sampleProvider, (IDisposable)disposables);
AddMixerInput(wrapper);
}
示例11: BuildConnectionToken
public IObservable<ConnectionToken> BuildConnectionToken(string host, int port, IScheduler scheduler)
{
return Observable.Create<ConnectionToken>(observer =>
{
var disposable = new CompositeDisposable();
var socket = socketFactory();
var socketEvent = socketEventsFactory();
socketEvent.RemoteEndPoint = new DnsEndPoint(host, port);
socketEvent.SocketClientAccessPolicyProtocol = System.Net.Sockets.SocketClientAccessPolicyProtocol.Tcp;
var connectionToken = new ConnectionToken(socket, socketEvent);
var disposableEventSubscription = socketEvent.Completed.Subscribe(_ =>
{
SendNotificationToObserver(observer, socketEvent, connectionToken);
});
var disposableActions = scheduler.Schedule(() =>
{
if (!socket.ConnectAsync(socketEvent))
{
SendNotificationToObserver(observer, socketEvent, connectionToken);
}
});
disposable.Add(disposableEventSubscription);
disposable.Add(disposableActions);
return disposable;
});
}
示例12: Receive
public IObservable<string> Receive(Socket connectedSocket, SocketAsyncEventArgs socketEventArgs, IScheduler scheduler, byte[] buffer)
{
if (connectedSocket == null)
throw new ArgumentNullException("connectedSocket");
if (socketEventArgs == null)
throw new ArgumentNullException("socketEventArgs");
return Observable.Create<string>(observer =>
{
var disposable = new CompositeDisposable();
var disposableEventSubscription = socketEventArgs.CompletedObservable().ObserveOn(scheduler).Subscribe(_ =>
{
SendNotificationToObserver(observer, socketEventArgs);
});
var disposableActions = scheduler.Schedule(() =>
{
socketEventArgs.SetBuffer(buffer, 0, buffer.Length);
if (!connectedSocket.ReceiveAsync(socketEventArgs))
{
SendNotificationToObserver(observer, socketEventArgs);
}
});
disposable.Add(disposableEventSubscription);
disposable.Add(disposableActions);
return disposable;
});
}
示例13: SetUp
public void SetUp()
{
Clock.Reset();
disposables = new CompositeDisposable
{
Disposable.Create(Clock.Reset)
};
schedule = GetScheduleDelegate();
commandsScheduled = new ConcurrentBag<IScheduledCommand>();
commandsDelivered = new ConcurrentBag<IScheduledCommand>();
var configuration = new Configuration()
.TraceScheduledCommands() // trace to console
.TraceScheduledCommands(
onScheduling: _ => { },
onScheduled: c => commandsScheduled.Add(c),
onDelivering: _ => { },
onDelivered: c => commandsDelivered.Add(c));
Configure(configuration, d => disposables.Add(d));
disposables.Add(ConfigurationContext.Establish(configuration));
}
示例14: SendMessage
public IObservable<Unit> SendMessage(Socket connectedSocket, SocketAsyncEventArgs socketEventArgs, IScheduler scheduler, string message)
{
if (connectedSocket == null)
throw new ArgumentNullException("connectedSocket");
if (socketEventArgs == null)
throw new ArgumentNullException("socketEventArgs");
return Observable.Create<Unit>(observer =>
{
var disposable = new CompositeDisposable();
var buffer = Encoding.UTF8.GetBytes(message);
var disposableCompletedSubscription = socketEventArgs.CompletedObservable().ObserveOn(scheduler).Subscribe(_ =>
{
SendNotificationToObserver(observer, socketEventArgs);
});
var disposableActions = scheduler.Schedule(() =>
{
socketEventArgs.SetBuffer(buffer, 0, buffer.Length);
if (!connectedSocket.SendAsync(socketEventArgs))
{
SendNotificationToObserver(observer, socketEventArgs);
}
});
disposable.Add(disposableCompletedSubscription);
disposable.Add(disposableActions);
return disposable;
});
}
示例15: Connect
public IObservable<Socket> Connect(string host, int port, IScheduler scheduler)
{
return Observable.Create<Socket>(observer =>
{
var disposable = new CompositeDisposable();
var socket = socketFactory();
var socketEvent = socketEventsFactory();
socketEvent.RemoteEndPoint = new DnsEndPoint(host, port);
socketEvent.UserToken = socket;
var disposableEventSubscription = socketEvent.CompletedObservable().ObserveOn(scheduler).Subscribe(_ =>
{
SendNotificationToObserver(observer, socketEvent);
});
var disposableActions = scheduler.Schedule(() =>
{
if (!socket.ConnectAsync(socketEvent))
{
SendNotificationToObserver(observer, socketEvent);
}
});
disposable.Add(disposableEventSubscription);
disposable.Add(disposableActions);
return disposable;
});
}