📄 publisher.cs
字号:
using System;
using System.Collections;
using System.Data;
using Ticker;
namespace PublishSubscribe
{
/// <summary>
/// Publisher类发布FakeTickerList(实际上是订阅者的信息)
/// 继承自System.MarshalByRefObject类
/// 使得远程的“订阅者”可以跨越进程访问到它
/// Class Publisher publishes the FakeTickerList and inherits from
/// System.MarshalByRefObject so that subscribers can remotely access the class
/// across process boundaries
/// </summary>
public class Publisher : System.MarshalByRefObject
{
/// <summary>
/// 声明FakeTickerList列表(实际上是订阅者的消息列表)
/// An array list to hold the subscribed fake tickers
/// </summary>
public ArrayList FakeTickerList;
/// <summary>
///该变量保存订阅者的数目
/// Keep track of the number of subcriptions
/// </summary>
private int nNumSubscriptions;
/// <summary>
/// NumSubscrptions属性,设置订阅者数目
/// Get and set for the nmber of subscriptions
/// </summary>
public int NumSubscriptions
{
get
{
return nNumSubscriptions;
}
set
{
nNumSubscriptions = value;
}
}
/// <summary>
/// 声明Delegate对象PublishedTickerFunction,它带有两个参数
/// declare the definition for the function
/// </summary>
public delegate void PublishedTickerFunction( object SourceOfEvent, FakeTicker FakeTickerArg );
/// <summary>
/// 该实例最重要的部分之一:声明委托和事件绑定
/// declare the event to be triggered when a fake ticker is updated
/// </summary>
public event PublishedTickerFunction OnUpdatedFakeTicker;
public void PublishNewEvents()
{
///如果委托不为空
///
/// if the event is not null ( ie something has registered )
/// publish the Faketicker list
if( OnUpdatedFakeTicker != null )
{
foreach( FakeTicker fake in FakeTickerList )
{
///产生新值
///give it a new value
fake.Calculate();
/// 发布事件,OnUpdatedFakeTicker是一个事件列表
/// publish it
OnUpdatedFakeTicker( this, fake );
}
}
}
public Publisher()
{
//
// TODO: Add constructor logic here
//
FakeTickerList = new ArrayList();
}
/// <summary>
///注册一个用户
///将可被订阅者(subscrber)调用。
/// Register a ticker with the Publish class
/// </summary>
/// <param name="ticker">The FakeTicker to be registered</param>
public void RegisterFakeTicker( string strName, int nValue )
{
///创建新的faketicker实例
///create a new faketicker
FakeTicker Fake = new FakeTicker( strName, nValue );
///将它加入到列表中
///store it in the update list
FakeTickerList.Add( Fake );
nNumSubscriptions++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -