工程中很多地方用异步线程处理 返回在界面显示,如果界面更新比较少的话,可以单个封装Delegate用Invoke调用,
如果地方过多.就需要 UI主线程轮训,以及 lazy Update了(就是如果在同一帧里面,如果多次调用UpdateXXX,只更新一次)
解决 UI跨线程更新的问题
直接上代码了:
public partial class Form1 : Form { public delegate void mainThreadTickDelegate(); private static Form1 _ins; public Form1() { InitializeComponent(); _ins = this; } private void Form1_Load(object sender, EventArgs e) { Task _t = new Task(_mainThreadTick); _t.Start(); } public mainThreadTickDelegate Event_MainThreadTick; void _mainThreadTick() { //log("_mainThreadTick"); while (true) { updateClientServicesViewdelegate d = new updateClientServicesViewdelegate(_ins._mainThreadTickInstance); _ins.Invoke(d); Thread.Sleep(1000); } } void _mainThreadTickInstance() { //log("_mainThreadTickInstance"); if (Event_MainThreadTick != null) { Event_MainThreadTick(); } _updateClientServicesView(); } bool _needUpdateClientServicesView = true; void _updateClientServicesView() { if (!_needUpdateClientServicesView) { return; } _needUpdateClientServicesView = false; //后续更新逻辑 } }