一个完整的操作UI线程的例子,用到了多种方式
生活随笔
收集整理的這篇文章主要介紹了
一个完整的操作UI线程的例子,用到了多种方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace CroessThread更新文本
{public partial class Form1 : Form{public Form1(){InitializeComponent();}delegate void invokeCallBackSetTextDelegent(string content); //代理 /// <summary>/// 多線程調用控件正確的方法/// </summary>/// <param name="content"></param>private void OperationControl(object content){string convertObjTocontent = content as string;if (this.txtMrid.InvokeRequired)//{invokeCallBackSetTextDelegent fc = new invokeCallBackSetTextDelegent(OperationControl); //this.Invoke(fc);//通過代理調用刷新方法}else{this.txtMrid.Text = this.txtMrid.Text + "," + convertObjTocontent;}}/// <summary>/// 多線程調用控件正確的方法/// </summary>/// <param name="content"></param>private void OperationControl(string content){string convertObjTocontent = content as string;if (this.txtMrid.InvokeRequired)//{invokeCallBackSetTextDelegent fc = new invokeCallBackSetTextDelegent(OperationControl); //this.Invoke(fc,content);//通過代理調用刷新方法}else{this.txtMrid.Text = this.txtMrid.Text + "," + convertObjTocontent;}}/// <summary>/// 多線程調用控件正確的方法/// </summary>/// <param name="content"></param>private bool OperationControlWithReturn(object content){string convertObjTocontent = content as string;if (this.txtMrid.InvokeRequired)//{invokeCallBackSetTextDelegent fc = new invokeCallBackSetTextDelegent(OperationControl); //this.Invoke(fc, content);//通過代理調用刷新方法}else{this.txtMrid.Text = this.txtMrid.Text + "," + convertObjTocontent;}return true;}/// <summary>/// 更新文本/// </summary>/// <param name="txtMrid"></param>/// <param name="txtName"></param>/// <param name="txtState"></param>private void SetTextSafe(string txtMrid, string txtName, string txtState){if (this.txtMrid.InvokeRequired){//方法一 缺陷 容易參數范圍擴大提升或是減小this.txtMrid.Invoke(new MethodInvoker(delegate { this.txtMrid.Text = txtName; })); // 匿名委托 無參數, 不用創(chuàng)建不用定義委托類型和委托方法體//方法二 缺陷WaitCallback callBack = new WaitCallback(OperationControl); //用創(chuàng)建WaitCallback的 委托this.txtMrid.Invoke(callBack, new object[] { txtMrid }); // 主動型 //方法三 (缺陷的方法,注入到主線程 )//將代理綁定到方法 invokeCallBackSetTextDelegent fc = new invokeCallBackSetTextDelegent(OperationControl); // 同方法二,只不過是自定義需要自己創(chuàng)建不用定義委托類型this.Invoke(fc, new object[] { txtMrid });//調用代理//方法五 invokeCallBackSetTextDelegent synacall = new invokeCallBackSetTextDelegent(OperationControl); // 同方法二完全一樣AsyncCallback callback = new AsyncCallback(SynacalCallBack);IAsyncResult result = synacall.BeginInvoke(txtMrid, callback, null);//異步版本的synacall.EndInvoke(result);//方法六 Action<string> actAnoi = delegate(string arg) { OperationControl(arg); }; //不用定義委托類型 actAnoi(txtMrid);//方法六 Action<string> actAname =new Action<string>(OperationControl); //不用定義委托類型 actAnoi(txtMrid);//方法七Func<string, bool> func = new Func<string, bool>(OperationControlWithReturn); //不用定義委托類型 func.Invoke(txtMrid);}else{this.txtMrid.Text = txtMrid;}}private void button1_Click(object sender, EventArgs e){List<string> lsstr = new List<string>();lsstr.Add("同志們");lsstr.Add("大家好");lsstr.Add("早上好");ParameterizedThreadStart delstart = new ParameterizedThreadStart(SetTextCommon);Thread thread = new Thread(delstart);thread.Start(lsstr);}private void SetTextCommon(object lsObje){List<string> lsstring = lsObje as List<string>;SetTextSafe(lsstring[0], string.Empty, string.Empty);}private void SynacalCallBack(IAsyncResult result){Console.WriteLine("完成 ");}}
}
轉載于:https://www.cnblogs.com/lixinhai/archive/2012/06/08/2541311.html
總結
以上是生活随笔為你收集整理的一个完整的操作UI线程的例子,用到了多种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu升级之后The disk d
- 下一篇: Next-best-action mar