本文整理汇总了C#中Subscriber.Subscribe方法的典型用法代码示例。如果您正苦于以下问题:C# Subscriber.Subscribe方法的具体用法?C# Subscriber.Subscribe怎么用?C# Subscriber.Subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subscriber
的用法示例。
在下文中一共展示了Subscriber.Subscribe方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Subscriber
public void EventでUnsubscribe()
{
var subscriber = new Subscriber();
int callNum1 = 0;
int callNum2 = 0;
int callNum3 = 0;
subscriber.Subscribe("topic1", () => { callNum1++; });
var e = subscriber.Subscribe("topic1", () => { callNum2++; });
subscriber.Subscribe("topic2", () => { callNum3++; });
subscriber.Call("topic1");
subscriber.Call("topic2");
subscriber.Call("topic1");
subscriber.Call("topic2");
subscriber.Unsubscribe(e);
subscriber.Call("topic1");
subscriber.Call("topic2");
subscriber.Call("topic1");
subscriber.Call("topic2");
Assert.AreEqual(callNum1, 4);
Assert.AreEqual(callNum2, 2);
Assert.AreEqual(callNum3, 4);
}
示例2: Main
static void Main()
{
Timer timer = new Timer();
Subscriber sub = new Subscriber();
sub.Subscribe(timer);
timer.RepeatWithDelegate(Subscriber.PrintHeart, 1,5);
}
示例3: Main
static void Main(string[] args)
{
string connectionString = "MyConnectionString";
const string topicName = "shopify-notifications";
Publisher publisher = new Publisher(topicName, connectionString);
Subscriber subscriber = new Subscriber(topicName, connectionString);
subscriber.Subscribe<SampleEvent>("SampleEvents", message => Console.WriteLine(
String.Format("received message with id \"{0}\" and content \"{1}\"", message.MessageId,
message.GetBody().Message)));
while (true)
{
Console.WriteLine("Write something to send a message:");
string message = Console.ReadLine();
if (!string.IsNullOrEmpty(message))
{
var publishMessage = PublishMessage.Create(new SampleEvent(message), Guid.NewGuid().ToString());
publisher.Send(publishMessage);
}
}
}
示例4: Call
public void Call()
{
var subscriber = new Subscriber();
int callNum1 = 0;
int callNum2 = 0;
int callNum3 = 0;
subscriber.Subscribe("topic1", () => { callNum1++; });
subscriber.Subscribe("topic1", () => { callNum2++; });
subscriber.Subscribe("topic2", () => { callNum3++; });
subscriber.Call("topic1");
subscriber.Call("topic2");
subscriber.Call("topic1");
subscriber.Call("topic2");
Assert.AreEqual(callNum1, 2);
Assert.AreEqual(callNum2, 2);
Assert.AreEqual(callNum3, 2);
}
示例5: SetModel2
private static void SetModel2(int index)
{
var sub = _model2Subscribers.ElementAtOrDefault(index);
if (sub != null)
{
sub.Dispose();
Console.WriteLine("Disposed Model2: " + index);
}
sub = new Subscriber<Model2>();
sub.BindOnNext((data) => Console.WriteLine(OutputFormat, "Model2 " + index, data.Name, data.Message));
sub.Subscribe(PublisherFactory.Instance.Resolve<Model2>());
_model2Subscribers.Add(sub);
}
示例6: Mute
public void Mute()
{
var subscriber = new Subscriber();
int callNum1 = 0;
subscriber.Subscribe("topic1", () => { callNum1++; });
subscriber.Call("topic1");
Assert.AreEqual(callNum1, 1);
subscriber.Mute("topic1");
subscriber.Call("topic1");
Assert.AreEqual(callNum1, 1);
}
示例7: Main
public static void Main()
{
Group provider = new Group();
Subscriber observer1 = new Subscriber("User: Jovan Vulin");
Subscriber observer2 = new Subscriber("User: John Doe");
provider.handlePost(100, "Welcome users!", true);
observer1.Subscribe(provider);
observer2.Subscribe(provider);
provider.handlePost(101, "C# patterns", true);
//Invalid post, shouldn't be displayed
provider.handlePost(100, "Welcome users!", true);
provider.handlePost(100, "Test users!", true);
provider.handlePost(100);
provider.handlePost(102, "Observer pattern", true);
Console.ReadLine();
}
示例8: Subscribe_Success
public void Subscribe_Success()
{
var scheduler = new TestScheduler();
var observer = scheduler.CreateObserver<std_msgs.String>();
var publisher = scheduler.CreateHotObservable(OnNext(10, new std_msgs.String() {data = "test data"}));
MSlaveClient.ConstructorUri = (t1, t2) => { };
MSlaveClient.AllInstances.RequestTopicAsyncStringStringListOfProtocolInfo =
(t1, t2, t3, t4) => Task.Factory.StartNew(() => new TopicParam() {HostName = "localhost", PortNumber = 12345, ProtocolName = "TCPROS"});
MRosTopicServer<std_msgs.String>.ConstructorStringStringUri = (t1, t2, t3, t4) => { };
MRosTopicServer<std_msgs.String>.AllInstances.StartAsyncTopicParamBoolean =
(t1, t2, t3) => Task.Factory.StartNew(() => (IObservable<std_msgs.String>)publisher);
var sub = new Subscriber<std_msgs.String>("testtopic", "test");
(sub as ISubscriber).UpdatePublishers(new List<Uri>() {new Uri("http://localhosst")});
sub.Subscribe(observer);
scheduler.AdvanceBy(100);
observer.Messages.First().Value.Value.data.Is("test data");
}
示例9: DisposeTest
public void DisposeTest()
{
var freeAll = new Action(() =>
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.Collect(0, GCCollectionMode.Forced);
GC.Collect(1, GCCollectionMode.Forced);
GC.Collect(2, GCCollectionMode.Forced);
});
freeAll();
var memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
Debug.WriteLine("Begin: " + (memBegin / 1000000));
var p1_1 = new Publisher();
var p2_1 = new Publisher();
var s1_1 = new Subscriber();
var s2_1 = new Subscriber();
s1_1.Subscribe(p1_1);
s2_1.Subscribe(p2_1);
// Everything is disposed
p1_1 = null;
p2_1 = null;
s1_1 = null;
s2_1 = null;
freeAll();
var afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("All freed (should be around 0MB): " + (afterDispose / 1000000));
memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
var p1_2 = new Publisher();
var p2_2 = new Publisher();
var s1_2 = new Subscriber();
var s2_2 = new Subscriber();
s1_2.Subscribe(p1_2);
s2_2.Subscribe(p2_2);
// Now we try to dispose subscriber 2
// It is not freed, because it has reference to publisher 2 that was not disposed.
p1_2 = null; // dispose publisher 1
s1_2 = null; // dispose subscriber 1
//p2 = null; // publisher 2 not disposed
s2_2 = null; // dipose subscriber 2
freeAll();
afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("Only publisher 1 and subscriber 1 are freed (should be around 200MB): " + (afterDispose / 1000000));
memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
var p1_3 = new Publisher();
var p2_3 = new Publisher();
var s1_3 = new Subscriber();
var s2_3 = new Subscriber();
s1_3.Subscribe(p1_3);
s2_3.Subscribe(p2_3);
// Dispose publisher 2, but do not dispose subsriber 2
// publisher 2 is disposed, eventhough subscriber 2 is attached to it's event
p1_3 = null; // dispose publisher 1
p2_3 = null; // dispose subscriber 1
s1_3 = null; // dispose publisher 2
//s2 = null; // subscriber 2 not disposed
freeAll();
afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("Only subscriber 2 is not freed (should be around 100MB): " + (afterDispose / 1000000));
}
示例10: Event_Subscriber_Leak
public void Event_Subscriber_Leak()
{
freeAll();
var memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
Debug.WriteLine("Begin: " + (memBegin / 1000000));
var p1_1 = new Publisher();
var p2_1 = new Publisher();
var s1_1 = new Subscriber();
var s2_1 = new Subscriber();
s1_1.Subscribe(p1_1);
s2_1.Subscribe(p2_1);
// Everything is disposed
p1_1 = null;
p2_1 = null;
s1_1 = null;
s2_1 = null;
freeAll();
var afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("All freed (should be around 0MB): " + (afterDispose / 1000000));
memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
var p1_2 = new Publisher();
var p2_2 = new Publisher();
var s1_2 = new Subscriber();
var s2_2 = new Subscriber();
s1_2.Subscribe(p1_2);
s2_2.Subscribe(p2_2);
// Now we try to dispose subscriber 2
// It is not freed, because it has reference to publisher 2 that was not disposed.
p1_2 = null; // dispose publisher 1
s1_2 = null; // dispose subscriber 1
//p2 = null; // publisher 2 not disposed
s2_2 = null; // dipose subscriber 2
freeAll();
afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("Only publisher 1 and subscriber 1 are freed (should be around 200MB): " + (afterDispose / 1000000));
memBegin = Process.GetCurrentProcess().PrivateMemorySize64;
var p1_3 = new Publisher();
var p2_3 = new Publisher();
var s1_3 = new Subscriber();
var s2_3 = new Subscriber();
s1_3.Subscribe(p1_3);
s2_3.Subscribe(p2_3);
// Dispose publisher 2, but do not dispose subsriber 2
// publisher 2 is disposed, eventhough subscriber 2 is attached to it's event
p1_3 = null; // dispose publisher 1
p2_3 = null; // dispose subscriber 1
s1_3 = null; // dispose publisher 2
//s2 = null; // subscriber 2 not disposed
freeAll();
afterDispose = Process.GetCurrentProcess().PrivateMemorySize64 - memBegin;
Debug.WriteLine("Only subscriber 2 is not freed (should be around 100MB): " + (afterDispose / 1000000));
}