public AsyncUpdateMessageHelper(Control invokeControl, UpdateMessageDelegate updateMessage) { InvokeControl = invokeControl; UpdateMessage = updateMessage; AsyncFormEventBus.Register <AsyncFormMessageEvent>(UpdateUi); WithPrefix = true; AutoAppendLine = true; }
protected AsyncForm() { if (!this.DesignMode) { //抽象类会导致Vs设计器无法显示的问题... _showStateCallback = ShowCallbackMessage; AsyncFormEventBus.Register <AsyncFormMessageEvent>(UpdateUi); WithPrefix = true; } }
//此方法支持在自动判断是否在非创建线程中被调用 private void UpdateUi(AsyncFormMessageEvent messageEvent) { if (!AsyncFormEventBus.ShouldRaise()) { return; } string value = string.Format("{0}{1}", messageEvent.Message, AutoAppendLine ? Environment.NewLine : string.Empty); if (WithPrefix) { value = messageEvent.DateTimeEventOccurred.ToString("yyyy-MM-dd HH:mm:ss:fff") + " => " + value; } if (InvokeControl.InvokeRequired) { InvokeControl.Invoke(UpdateMessage, value); } else { UpdateMessage(value); } }
//此方法会在非UI线程中被调用 private void UpdateUi(AsyncFormMessageEvent obj) { if (!AsyncFormEventBus.ShouldRaise()) { return; } string value = string.Format("{0}{1}", obj.Message, Environment.NewLine); if (WithPrefix) { value = obj.DateTimeEventOccurred.ToString("yyyy-MM-dd HH:mm:ss:fff") + " => " + value; } var invoker = GetInvoker(); if (invoker.InvokeRequired) { invoker.Invoke(_showStateCallback, value); } else { _showStateCallback.Invoke(value); } }