• C#:多线程使用TextBox控件


    Posted on 2008-06-23 14:39 SmartStone 阅读(1334) 评论(2) 编辑 收藏 
            //声明一个委托
            public delegate void SetTextBoxValue(string value);

            
    //委托使用文本框
            void SetMyTextBoxValue(string value)
            
    {
                
    // Control.InvokeRequired 属性: 获取一个值,该值指示调用方在对控件进行方法调用时是否必须调用 Invoke 方法,因为调用方位于创建控件所在的线程以外的线程中。当前线程不是创建控件的线程时为true,当前线程中访问是False
                if (this.TextBoxControl.InvokeRequired)
                
    {
                    SetTextBoxValue objSetTextBoxValue 
    = new SetTextBoxValue(SetMyTextBoxValue);

                    
    // IAsyncResult 接口:表示异步操作的状态。不同的异步操作需要不同的类型来描述,自然可以返回任何对象。
                    
    // Control.BeginInvoke 方法 (Delegate):在创建控件的基础句柄所在线程上异步执行指定委托。
                    IAsyncResult result = this.TextBoxControl.BeginInvoke(objSetTextBoxValue, new object[]{ value });
                    
    try {
                        objSetTextBoxValue.EndInvoke(result);
                    }

                    
    catch {
                    }

                }

                
    else
                
    {
                    
    this.TextBoxControl.Text += value + Environment.NewLine;
                    
    this.TextBoxControl.SelectionStart = this.TextBoxControl.TextLength;
                    
    this.TextBoxControl.ScrollToCaret();
                }

            }


    示例代码:

            public TestForm()
            
    {
                InitializeComponent();
            }


            
    private delegate void SetTextBoxValue(string value);

            
    private void SetMyTextBoxValue(string value)
            
    {
                
    if (this.TextBoxControl.InvokeRequired)
                
    {
                    SetTextBoxValue objSetTextBoxValue 
    = new SetTextBoxValue(SetMyTextBoxValue);
                    IAsyncResult result 
    = this.TextBoxControl.BeginInvoke(objSetTextBoxValue, new object[] { value });
                    
    try
                    
    {
                        objSetTextBoxValue.EndInvoke(result);
                    }

                    
    catch
                    
    {
                    }

                }

                
    else
                
    {
                    
    this.TextBoxControl.Text += value + Environment.NewLine;
                    
    this.TextBoxControl.SelectionStart = this.TextBoxControl.TextLength;
                    
    this.TextBoxControl.ScrollToCaret();
                }

            }


            
    private void ExecuteNewThread()
            
    {
                
    for (int i = 0; i < 1000; i++)
                
    {
                    SetMyTextBoxValue(i.ToString());
                }

            }


            
    private void NewThreadButton_Click(object sender, EventArgs e)
            
    {
                Thread objThread 
    = new Thread(new ThreadStart(ExecuteNewThread));
                objThread.IsBackground 
    = true;
                objThread.Start();
            }

        }
  • 相关阅读:
    ceph 网络配置
    Centos7.2 下DNS+NamedManager高可用部署方案完整记录
    Mysql多实例数据库
    Mysql 基础
    搭建本地YUM仓库
    Go实现线程安全的缓存
    KubeEdge安装详细教程
    Kubeedge实现原理
    Go语言中new()和make()的区别
    Go语言中append()函数的源码实现在哪里?
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/2443789.html
Copyright © 2020-2023  润新知