适合C# Actor的消息执行方式(6):协变与逆变
2010-09-30 20:50:09 来源:WEB开发网为了让Monior支持查询,我们还需要为它添加这样的代码:
public class Monitor : Actor<Action<Monitor>>, IPort<Monitor>,
ICrawlResponseHandler,
IStatisticRequestHandelr
{
...
#region IStatisticRequestHandelr Members
void IStatisticRequestHandelr.GetCrawledCount(IPort<IStatisticResponseHandler> requester)
{
requester.Post(r => r.ReplyCrawledCount(this.m_urlContent.Count));
}
void IStatisticRequestHandelr.GetContent(IPort<IStatisticResponseHandler> requester, string url)
{
string content;
if (!this.m_urlContent.TryGetValue(url, out content))
{
content = null;
}
requester.Post(r => r.ReplyContent(url, content));
}
#endregion
}
最后,我们来尝试着使用这个“查询”功能。首先,我们编写一个测试用的TestStatisticPort类:
public class TestStatisticPort : IPort<IStatisticResponseHandler>, IStatisticResponseHandler
{
private IPort<IStatisticRequestHandelr> m_statisticPort;
public TestStatisticPort(IPort<IStatisticRequestHandelr> statisticPort)
{
this.m_statisticPort = statisticPort;
}
public void Start()
{
while (true)
{
Console.ReadLine();
this.m_statisticPort.Post(s => s.GetCrawledCount(this));
}
}
#region IPort<IStatisticResponseHandler> Members
void IPort<IStatisticResponseHandler>.Post(Action<IStatisticResponseHandler> message)
{
message(this);
}
#endregion
#region IStatisticResponseHandler Members
void IStatisticResponseHandler.ReplyCrawledCount(int count)
{
Console.WriteLine("Crawled: {0}", count);
}
void IStatisticResponseHandler.ReplyContent(string url, string content) { ... }
#endregion
}
- ››适合做商品团购营销的网站
- ››适合所有浏览器hack的CSS技巧
- ››消息称中国移动即将获得iPhone 4销售权
- ››适合C# Actor的消息执行方式(1):Erlang中的模式...
- ››适合C# Actor的消息执行方式(2):C# Actor的尴尬...
- ››适合C# Actor的消息执行方式(3):中看不中用的解...
- ››适合C# Actor的消息执行方式(4):阶段性总结
- ››适合C# Actor的消息执行方式(5):一个简单的网络...
- ››适合C# Actor的消息执行方式(6):协变与逆变
- ››适合1-5个月经验的seo优化全过程分享
- ››消息称联通高层赴美谈引入iPhone4 将带WiFi
- ››消息称微软将在近期发布IE9 beta
更多精彩
赞助商链接