c#事件的写法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using XCore.Devices.Socket;
namespace EventDemo
{
// 步骤
// 1.创建事件参数类
public partial class Form1 : Form
{
borderboxSocketClient socketClint =new SocketClient();
public Form1()
{
InitializeComponent();
//Counter e = new Counter();
/
/e.NumberReached += OnNumberReached;
//e.Add();
//初始化socket
socketClint.InitSocket ("192.168.3.2",1000);
//socket开始
socketClint.Start();
//socket接收事件挂载
socketClint.OnSocketMessagePushed += OnSocketMessagePushed;
}
private void OnSocketMessagePushed(object sender, OnPushSocketServerMessageArgs e) {
string ReceiveBuffer = BitConverter.ToString(e.ReceiveBuffer);
/
/string ReceiveBuffer = Encoding.ASCII.GetString(e.ReceiveBuffer);
Console.WriteLine(ReceiveBuffer);
}
private void OnNumberReached(object sender, ReachedEventArgs e)
{
Console.WriteLine(string.Format("[{0}] {1}", e.DateTime, e.Number));
}
}
/// <summary>
/// 事件参数类(包含事件触发时所需传递的参数) 继承EventArgs
/// </summary>
public class ReachedEventArgs : EventArgs
{
public DateTime DateTime {get;set;}
public int Number {get;set;}
}
public class Counter
{
public EventHandler<ReachedEventArgs> NumberReached;
protected virtual void OnNumberReached(ReachedEventArgs e)
{
EventHandler<ReachedEventArgs> handle = NumberReached;
EventHandler<ReachedEventArgs> handle = NumberReached;
if(handle !=null)
{
handle(this, e);
}
}
public void Add()
{
int total =0;
for(int i =0; i <5; i++)
{
total += i;
if(total >=6)
{
ReachedEventArgs args =new ReachedEventArgs();
args.DateTime = DateTime.Now;
args.Number = i;
OnNumberReached(args);
}
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论