本文整理汇总了C#中CompositeDisposable.Add方法的典型用法代码示例。如果您正苦于以下问题:C# CompositeDisposable.Add方法的具体用法?C# CompositeDisposable.Add怎么用?C# CompositeDisposable.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CompositeDisposable
的用法示例。
在下文中一共展示了CompositeDisposable.Add方法的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: 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;
}
}
示例3: 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;
});
}
示例4: 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;
});
}
示例5: 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;
});
}
示例6: 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;
});
}
示例7: 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));
}
示例8: 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;
});
}
示例9: SetUp
public void SetUp()
{
disposables = new CompositeDisposable();
Command<Order>.AuthorizeDefault = (order, command) => true;
configuration = GetConfiguration();
disposables.Add(ConfigurationContext.Establish(configuration));
disposables.Add(configuration);
}
示例10: ShowView
public IDisposable ShowView(FrameworkElement view) {
var disp = new CompositeDisposable();
disp.Add(scheduler.Schedule(() => {
disp.Add(showView(view));
}));
return Disposable.Create(() => {
scheduler.Schedule(() => {
disp.Dispose();
});
});
}
示例11: Tail
//public IObservable<string> TailV2(IFile file)
//{
// return ObservableFile.WatchLines(file.GetNativePath(), Encoding.Default);
//}
public IObservable<string> Tail(IFile file, IScheduler scheduler = null)
{
return Observable.Create<string>(subj =>
{
var disposable = new CompositeDisposable();
scheduler = scheduler ?? RxApp.TaskpoolScheduler;
var abortSignal = new ManualResetEvent(false);
disposable.Add(Disposable.Create(() => abortSignal.Set()));
disposable.Add(scheduler.Schedule(abortSignal, (sched, state) =>
{
using (var reader = new StreamReader(
file.GetContent().OpenStream(FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
long lastOffset = reader.BaseStream.Length;
if (reader.BaseStream.Length > 0)
{
// Send the last 10 kb of text to the reader.
lastOffset = Math.Max(0, reader.BaseStream.Length - (1024 * 10));
}
while (!state.WaitOne(100))
{
// Idle if file hasn't changed.
if (reader.BaseStream.Length <= lastOffset)
{
if (reader.BaseStream.Length < lastOffset)
{
lastOffset = reader.BaseStream.Length;
}
continue;
}
// Read the data.
reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
var delta = reader.BaseStream.Length - lastOffset;
var buffer = new char[delta];
reader.ReadBlock(buffer, 0, buffer.Length);
// Publish the data.
subj.OnNext(new string(buffer));
// Update the offset.
lastOffset = reader.BaseStream.Position;
}
}
return Disposable.Empty;
}));
return disposable;
});
}
示例12: AddTwice
public void AddTwice()
{
int i = 0;
var d = new CompositeDisposable ();
var item = Disposable.Create (() => i++);
d.Add (item);
d.Add (item);
// this results in two items registered.
Assert.AreEqual (2, d.Count, "#1");
d.Dispose ();
// though, since the first disposal takes effect, it never invokes Dispose() on item twice.
Assert.AreEqual (1, i, "#2");
}
示例13: Schedule
/// <summary>
/// Schedules action to be executed after dueTime.
/// </summary>
public IDisposable Schedule(Action action, TimeSpan dueTime)
{
if (action == null)
throw new ArgumentNullException("action");
var dt = Scheduler.Normalize(dueTime);
var g = new CompositeDisposable();
g.Add(ThreadPoolScheduler.Instance.Schedule(() => g.Add(Schedule(action)), dt));
return g;
}
示例14: 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));
}
}
}
示例15: AddAfterDisposal
public void AddAfterDisposal()
{
int i1 = 0, i2 = 0;
var d = new CompositeDisposable ();
d.Add (Disposable.Create (() => i1++));
d.Dispose ();
Assert.AreEqual (1, i1, "#1");
d.Dispose ();
Assert.AreEqual (1, i1, "#2"); // no further addition
d.Add (Disposable.Create (() => i2++));
Assert.AreEqual (1, i2, "#3");
d.Dispose ();
Assert.AreEqual (1, i2, "#4"); // no further addition
d.Add (Disposable.Create (() => i2++)); // should be immediately disposed
Assert.AreEqual (2, i2, "#5");
}