扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:谢启东编译 来源:天极开发 2007年11月14日
关键字:
using namespace System; public ref struct Server { /*1*/ delegate void NewMsgEventHandler(String^ msg); /*2*/ static event NewMsgEventHandler^ ProcessNewMsg; /*3*/ static void Broadcast(String^ msg) { if (ProcessNewMsg != nullptr) { ProcessNewMsg(msg); } } }; |
using namespace System; public ref class Client { String^ clientName; /*4*/ void ProcessNewMsg(String^ msg) { Console::WriteLine("Client {0} received message {1}", clientName, msg); } public: Client(String^ clientName) { this->clientName = clientName; /*5*/ Server::ProcessNewMsg += gcnew Server::NewMsgEventHandler(this, &Client::ProcessNewMsg); } /*6*/ ~Client() { Server::ProcessNewMsg -= gcnew Server::NewMsgEventHandler(this, &Client::ProcessNewMsg); } }; |
using namespace System; int main() { Server::Broadcast("Message 1"); Client^ c1 = gcnew Client("A"); Server::Broadcast("Message 2"); Client^ c2 = gcnew Client("B"); Server::Broadcast("Message 3"); Client^ c3 = gcnew Client("C"); Server::Broadcast("Message 4"); c1->~Client(); Server::Broadcast("Message 5"); c2->~Client(); Server::Broadcast("Message 6"); c3->~Client(); Server::Broadcast("Message 7"); } |
Client A received message Message 2 Client A received message Message 3 Client B received message Message 3 Client A received message Message 4 Client B received message Message 4 Client C received message Message 4 Client B received message Message 5 Client C received message Message 5 Client C received message Message 6 |
public ref struct Server { // ... static event NewMsgEventHandler^ ProcessNewMsg { void add(NewMsgEventHandler^ n) { /* ... */ } void remove(NewMsgEventHandler^ n) { /* ... */ } } // ... }; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者