学更好的别人,swift
作更好的本身。微信
——《微卡智享》app
前言dom
EventHandler简介ide

微卡智享工具
EventHandler就是一个事件处理器,将一个事件与处理事件的方法联系起来的一种机制。 说人话就是:我是小明,如今想邀请小红出去玩,小红说要吃完饭后才能出来。那原来设计这样的话,我须要定时去小红那看看她吃没吃完饭,这样一直等到她吃完后咱们再一块儿出去,而采用EventHandler委托的话,就是吃饭的事小红本身处理,等吃完后他发送一个消息通知我吃完了,而后咱们一块儿出去就好了。测试
EventHandler使用
# | 说明 |
---|---|
1 | 声明一个EventArgs的子类,传递参数 |
2 | 声明委托对象,执行方法,将方法绑定委托对象 |
3 | 开启EventHandler的委托 |
EventHandler实现flex

微卡智享ui
01this
建立EventArgs子类
建立一个testEvent的子类,继承自EventArgs,而后定义了一个字符串和一个整数类型,用于记录返回一内容和当前的ID。
namespace threaddemo{ public class testEvent :EventArgs { public string datastr;
public int id;
public testEvent(string str,int id) { datastr = str; this.id = id; } }}
02
定义接口,写接口实现方法
namespace threaddemo{ interface Inftest { event EventHandler<testEvent> DataReceived; void Stop(); void Start(); }}
using System;using System.Collections.Generic;using System.Drawing.Text;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;
namespace threaddemo{ public class CTest : Inftest,IDisposable { public int id;
private AutoResetEvent exitEvent;
private Thread thread;
private int waitTime = 100;
private bool disposed = false;
private bool IsRunning;
public int cs = 0; public CTest(int _id) { id = _id; exitEvent = new AutoResetEvent(false); thread = new Thread(ReadThreadRun); thread.IsBackground = true; }
public event EventHandler<testEvent> DataReceived;
public void Dispose() { // Dispose(true); GC.SuppressFinalize(this); }
protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { try { Stop();
} catch (Exception) {
} } disposed = true; } }
public void Start() { IsRunning = true; thread.Start(); }
public void Stop() { IsRunning = false; exitEvent.Reset(); exitEvent.Set(); RaiseDataReceived("手动中止"); }
public void Reset() { exitEvent.Reset(); exitEvent.Set(); }
public void setcs(int _cs) { cs = _cs;
}
private void RaiseDataReceived(string msg) { DataReceived?.Invoke(this, new testEvent(msg, id)); }
/// <summary> /// 接收线程 /// </summary> private void ReadThreadRun() { while (IsRunning) { try { if (exitEvent.WaitOne(waitTime)) { if (IsRunning) { Thread.Sleep(1000); id++; if (id % 5 == 0) { throw new Exception("余数:0"); }else if (id % 5 == 1) { try { throw new Exception("故意出错"); } catch(Exception ex) { RaiseDataReceived(ex.Message); Reset(); } } RaiseDataReceived("状态:重启"); } else { Thread.Sleep(1000); RaiseDataReceived("中止"); } }
Random rd = new Random(); int count = rd.Next(0, 13); if (count < 10) { RaiseDataReceived(count.ToString()); } else if (count == 10) { throw new Exception("throw 数字:" + count); } else { RaiseDataReceived("数字:" + count + " Reset"); Reset(); } } catch (Exception ex) { RaiseDataReceived("error " + ex.Message); Reset(); }
//Thread.Sleep(100); } } }}
03
开启委托
private void _test_DataReceived(object sender, testEvent e) {
try { TextShowNew(richTextBox1, e.datastr); int count = -1; try { count = Convert.ToInt32(e.datastr); } catch(Exception ex) { count = -1; }
if (count >= 0) { CItem item = new CItem(0, count, "测试" + count, DateTime.Now); ht.checkHashTest(item, 5000); }
if (!ht.checkDatetime(dtclear, 5000)) { ht.RemoveHashTableFromTime(5000); dtclear = DateTime.Now; } } catch (Exception ex) { TextShowNew(richTextBox1, ex.Message); } }
private void Openinf() { try { if (_test != null) { _test.Stop(); _test.Dispose(); } _test = null; _test = new CTest(idx); _test.DataReceived += _test_DataReceived; _test.Start(); TextShowNew(richTextBox1, idx + "开启"); idx++; } catch (Exception ex) { TextShowNew(richTextBox1, "Error:" + ex.Message, 1); } }

完
扫描二维码
获取更多精彩
微卡智享

「 往期文章 」
本文分享自微信公众号 - 微卡智享(VaccaeShare)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。