/// <summary> /// 跨线程刷新控件 /// </summary> /// <param name="control">需要刷新的控件</param> private void RefreshControl(Control control) { if (control.InvokeRequired) { AsyncRefresh asr = RefreshControl; control.Invoke(asr, new object[] { control }); } else { control.Refresh(); } }
private async void RefreshTimer_Tick(object sender, EventArgs e) { AsyncRefresh asyncrefresh = new AsyncRefresh(ChatRoomService.GetMessages); // 1/2 //asyncrefresh.BeginInvoke(CallBack, null); // 1.AMP // 2.TAP IEnumerable <MessageInfo> messageInfos = await Task.Factory.FromAsync(asyncrefresh.BeginInvoke, asyncrefresh.EndInvoke, null); StringBuilder strText = new StringBuilder(); foreach (var message in messageInfos) { strText.AppendLine(string.Format("{0} {1} 说:", message.UserName, message.MessageTime)); strText.AppendLine(" " + message.MsgContent); strText.AppendLine(); } this.txtMessages.Text = strText.ToString(); }
public void CallBack(IAsyncResult result) { //AsyncRefresh asyncRefresh = result.AsyncState as AsyncRefresh;这一句和下面两句作用一致 AsyncResult asyncResult = result as AsyncResult; AsyncRefresh asyncRefresh = asyncResult.AsyncDelegate as AsyncRefresh; var messageInfos = asyncRefresh.EndInvoke(result); StringBuilder strText = new StringBuilder(); foreach (var message in messageInfos) { strText.AppendLine(string.Format("{0} {1} 说:", message.UserName, message.MessageTime)); strText.AppendLine(" " + message.MsgContent); strText.AppendLine(); } Action action = () => { this.txtMessages.Text = strText.ToString(); }; this.txtMessages.Invoke(action); }