遥远的青苹果

代码人生

 

一个完整的操作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; })); // 匿名委托 无参数, 不用创建不用定义委托类型和委托方法体

                //方法二 缺陷
                WaitCallback callBack = new WaitCallback(OperationControl); //用创建WaitCallback的 委托
                this.txtMrid.Invoke(callBack, new object[] { txtMrid }); // 主动型 

                //方法三 (缺陷的方法,注入到主线程 )
                //将代理绑定到方法 
                invokeCallBackSetTextDelegent fc = new invokeCallBackSetTextDelegent(OperationControl); // 同方法二,只不过是自定义需要自己创建不用定义委托类型
                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("完成 ");

        }


    }
}

posted on 2012-06-08 09:55  遥远的青苹果-李院长  阅读(207)  评论(0编辑  收藏  举报

导航