An event-driven communication c# program

5 篇文章 0 订阅

process communication can be analog to the theory of mobile network - there is a network that every subscriber can utilize to send and receive information.

of cause in program we should have resembles to network and user.

i have thought a lot on such kind of program implementation and structure, finally i found event driven or message driven is the best shape. to understand this concept one have to image there is a unique Centerlized static thing in your code - a postman. he is responsible for carrying everything for you and from you. there is no better choice. other implementation require tight coupling more or less, if every one talk to each other directly in code there should be at least n*(n-1) channel for this purpose. a centerlized - postman style - structure has a side good effect that it can written as message driven it mean the procedure can sleep until the message for him coming in. a static class called "Postman.cs" is defined in this quick demo written off duty tonight. this is also a must since every user want to see him clearly. this is a extreme situation where static class is the solution. i also wrote some Tcp class simulating Tcp communication mechanism, another Center.cs for simulating management entity since management part always have to look every thing. during implementation i found Broadcasting capability is the essence of communication protocol. in a microscopic view every communication path means a series of broadcasting in and only in useful scenario. since people have to register a mountain of handler function to the center. yes filter can be used to differentiate the sender object - but ovbiously i have to omit it for your dig. this is a working sample compile with vs2012 and run results as my expectation. it will be used in my machine talking project. which is on going. yeh.

here are output:

线程 'vshost.NotifyLoad' (0x2598) 已退出,返回值为 0 (0x0)。
线程 '<无名称>' (0x14c8) 已退出,返回值为 0 (0x0)。
线程 '<无名称>' (0x2570) 已退出,返回值为 0 (0x0)。
线程 'vshost.LoadReference' (0x2090) 已退出,返回值为 0 (0x0)。
“postman.vshost.exe”(托管(v4.0.30319)): 已加载“d:\mydocuments\visual studio 2012\Projects\postman\postman\bin\Debug\postman.exe”,符号已加载。
“postman.vshost.exe”(托管(v4.0.30319)): 已加载“C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll”
OnSend->Civil Send out data
OnSend->System.EventArgs100
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs100
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs123
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
线程 '<线程结束>' (0x6c0) 已退出,返回值为 0 (0x0)。
OnRecv->...
OnRecv->System.EventArgs456
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnRecv->...
OnRecv->System.EventArgs789
OnSend->System.EventArgs123
OnSend->System.EventArgs456
OnSend->System.EventArgs789
OnSend->System.EventArgs100
OnSend->Tcp send->58
OnSend->System.EventArgs123
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnSend->Tcp send->59
OnSend->System.EventArgs456
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
OnSend->Tcp send->60
OnSend->System.EventArgs789
OnRecv->System.EventArgs123
OnRecv->System.EventArgs456
OnRecv->System.EventArgs789
OnRecv->System.EventArgs100
单步执行: 正在逐过程执行不含符号“Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly”的方法
线程 'vshost.RunParkingWindow' (0x14a0) 已退出,返回值为 0 (0x0)。
单步执行: 正在逐过程执行不含符号“System.Threading.ExecutionContext.RunInternal”的方法
线程 '<无名称>' (0x26e0) 已退出,返回值为 0 (0x0)。
程序“[9692] postman.vshost.exe: 托管(v4.0.30319)”已退出,返回值为 0 (0x0)。
程序“[9692] postman.vshost.exe: 程序跟踪”已退出,返回值为 0 (0x0)。


here are code plain text:

---------------------------------------

Postman.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    static class Postman
    {
        public delegate void Handler(object sender, EventArgs args);
        public static void UpdatePostHandlers(object sender, string message) {
            if (null != PostHandlers) {
                PostHandlers(sender, new EventArgs());
            }
        }

        public static void UpdateSinkHandlers(object sender, string message) {
            if (null != SinkHandlers) {
                SinkHandlers(sender, new EventArgs());
            }
        }

        public static void RegPostHandler(Handler h)
        {
            PostHandlers += h;
        }
        public static void RegSinkHandler(Handler h)
        {
            SinkHandlers += h;
        }
        public static void DeRegPostHandler(Handler h)
        {
            PostHandlers -= h;
        }
        public static void DeRegSinkHandler(Handler h)
        {
            SinkHandlers -= h;
        }

        private static event Handler PostHandlers = null;
        private static event Handler SinkHandlers = null;
    }
}


---------------------------------------

Tcp.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;

    class Tcp
    {
        public int P { get; private set; }
        public Tcp(int port) {
            this.P = port;
            Postman.RegPostHandler(OnSend);
            Postman.RegSinkHandler(OnRecv);
        }

        public void OnSend(object sender, EventArgs args) { Debug.WriteLine("OnSend->" + args + this.P); }
        public void OnRecv(object sender, EventArgs args) { Debug.WriteLine("OnRecv->" + args + this.P); }

        public void Send(string msg) { Debug.WriteLine("OnSend->" + msg); this.OnSend(this, new EventArgs()); Postman.UpdateSinkHandlers(this, msg); }
        public void Recv(string msg) { Debug.WriteLine("OnRecv->..."); this.OnRecv(this, new EventArgs()); Postman.UpdatePostHandlers(this, msg); }

        ~Tcp()
        {
            Postman.DeRegPostHandler(OnSend);
            Postman.DeRegSinkHandler(OnRecv);
        }

    }
}


---------------------------------------

Center.cs


namespace postman
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;  
   
    // can be known as a subnet:)
    class Center
    {
                public int Q { get; private set; }
                public Center(int port)
                {
            this.Q = port;
            Postman.RegPostHandler(OnSend);
            Postman.RegSinkHandler(OnRecv);
        }

        public void OnSend(object sender, EventArgs args) { Debug.WriteLine("OnSend->" + args + this.Q); }
        public void OnRecv(object sender, EventArgs args) { Debug.WriteLine("OnRecv->" + args + this.Q); }

        public void Send(string msg) { Debug.WriteLine("OnSend->" + msg); this.OnSend(this, new EventArgs()); Postman.UpdateSinkHandlers(this, msg); }
        public void Recv(string msg) { Debug.WriteLine("OnRecv->..."); this.OnRecv(this, new EventArgs()); Postman.UpdatePostHandlers(this, msg); }
        ~Center()
        {
            Postman.DeRegPostHandler(OnSend);
            Postman.DeRegSinkHandler(OnRecv);
        }

    }
}
---------------------------------------

end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Event-driven programming is a programming paradigm that allows the software to respond to events or user actions in a asynchronous manner. Here are the general steps to implement an event-driven program: 1. Identify the events: Determine the events that the program needs to respond to. These could be user actions, system events, or external signals. 2. Define event handlers: Create functions or methods to handle each event. These functions will be executed when the event occurs. 3. Register event handlers: Associate each event handler with the corresponding event. This is usually done using a framework or library. 4. Run the program: Once the event handlers are registered, the program is ready to execute. As events occur, the associated event handlers will be executed. 5. Clean up: After the program finishes executing, clean up any resources used or allocated during the program. Here's an example of an event-driven program in Python using the Tkinter GUI library: ```python import tkinter as tk def button_click(event): print("Button clicked") root = tk.Tk() button = tk.Button(root, text="Click me") button.bind("<Button-1>", button_click) button.pack() root.mainloop() ``` In this program, we identify the event as a button click, define an event handler function `button_click` to handle the event, register the event handler with the button using the `bind` method, and run the program using the `mainloop` method. When the button is clicked, the `button_click` function will be executed and print "Button clicked" to the console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值