从其他类的WinForm控件的线程安全更新 [英] Thread-safe updates of a WinForm control from other classes

查看:137
本文介绍了从其他类的WinForm控件的线程安全更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有人请帮我以下的问题:

有两个类的MainForm和LWriter。下面是从LWriter的方法,除了在写入文件发送一些更新RichTextBox控件(通过mainForm.UpdateLog(文本))。一切正常,然而,这WriteOutput方法也做了一些在计算过程中冻结的形式广泛的处理。

我觉得WriteOutput应该被封装在一个单独的线程。可能有人请帮我解释如何将WriteOutput(LWriter类)的线程将调用mainForm.UpdateLog()从mainFrom以安全的方式?

我是新来的线程,从而帮助将非常AP preciated。

 公共无效WriteOutput(字符串输出,链接[]链接)
{
   尝试 {
      使用(StreamWriter的SW =新的StreamWriter(输出)){
         为(中间体X = 1; X&所述; links.Length; X ++){
       ...
           sw.WriteLine(...);
           sw.Flush();
         }
         mainForm.UpdateLog(小于文字>);
      }
   }赶上(例外五){...}
}
 

解决方案

代理可用于线程安全的通话

勾选此 http://msdn.microsoft.com/en-us/库/ ms171728.aspx

  //此委托允许设置异步调用
    //一个TextBox控件的Text属性。
    委托无效SetTextCallback(字符串文本);

    //此方法说明了制造线程安全模式
    //调用在Windows窗体控件。
    //
    //如果调用线程是从线程不同的
    //创建文本框控件,这种方法创建一个
    // SetTextCallback并自称异步使用
    //调用方法。
    //
    //如果调用线程是相同创建线程
    // TextBox控件,Text属性是直接设置。

    私人无效的setText(文本字符串)
    {
        // InvokeRequired需要比较的线程ID
        //调用线程创建线程的线程ID。
        //如果这些线程是不同的,它返回真。
        如果(this.textBox1.InvokeRequired)
        {
            SetTextCallback D =新SetTextCallback(的setText);
            this.Invoke(四,新的对象[] {文本});
        }
        其他
        {
            this.textBox1.Text =文本;
        }
    }
 

Could someone please help me with the following problem:

There are two classes MainForm and LWriter. Below is a method from the LWriter that in addition to writing to a file sends some updates to a RichTextBox control (through mainForm.UpdateLog(text)). Everything works fine, however, this WriteOutput method also does some extensive processing that during the calculation freezes the form.

I think the WriteOutput should be encapsulated in a separate thread. Could someone please help me out explaining how to place WriteOutput (LWriter class) in a thread that will then call mainForm.UpdateLog() from the mainFrom in a safe manner?

I am new to threads, thus help would be much appreciated.

public void WriteOutput(string output, Links[] links)
{
   try {
      using (StreamWriter sw = new StreamWriter(output)) {
         for (int x= 1; x<links.Length;x++) {
       ...
           sw.WriteLine( ... );
           sw.Flush();                              
         }
         mainForm.UpdateLog(<text>);
      }
   } catch(Exception e) { ... }
}

解决方案

delegate can be used for Thread safe calls

Check this http://msdn.microsoft.com/en-us/library/ms171728.aspx

            // This delegate enables asynchronous calls for setting
    // the text property on a TextBox control.
    delegate void SetTextCallback(string text);

    // This method demonstrates a pattern for making thread-safe
    // calls on a Windows Forms control. 
    //
    // If the calling thread is different from the thread that
    // created the TextBox control, this method creates a
    // SetTextCallback and calls itself asynchronously using the
    // Invoke method.
    //
    // If the calling thread is the same as the thread that created
    // the TextBox control, the Text property is set directly. 

    private void SetText(string text)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {   
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            this.textBox1.Text = text;
        }
    }

这篇关于从其他类的WinForm控件的线程安全更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆