更新的WinForm从另一个线程_and_类控制 [英] Update WinForm Controls from another thread _and_ class

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

问题描述

我想提出一个WinForms程序,这需要单独的线程
对于可读性和可维护性,我已经分居的所有非GUI代码出到不同的班级。这个类也产生另一个类,其中做了一些处理。不过,我现在已经运行到哪里,我需要改变来自于不同的类启动一个线程一个WinForms控制(字符串追加到文本框)问题



我有,四处搜寻,找到了不同的线程解决方案,并在不同的班级,但不能同时和提供的解决方案显得格格不入(对我)



这可能是最大的铅不过:如何从另一个更新UI螺纹另一类运行



类层次的例子:

 类的WinForm:表
{
...
服务器SERV =新服务器();
}

//服务器是在不同的线程的winform
级服务器
{
...
ClientConnection =新ClientConnection( );
}

//另一个新线程被创建来运行这个类
类ClientConnection
{
//想从这里修改的winform
}

据我了解,事件处理器可能是要走的路,但我不知道如何在这种情况下这样做(我也接受其他的建议;))



感谢任何帮助。


解决方案

从类要更新的形式并不重要。 WinForm的控制已到,他们创造在同一线程上进行更新。



因此​​,Control.Invoke,让你对自己的线程控制执行的方法。这也被称为异步执行,因为调用实际上是排队,并分别执行。



看这个的 /en-us/library/a1hetckb.aspx\">article,这个例子是类似的例子。在一个单独的线程一个单独的类更新窗体上的列表框。



-----更新
在这里,你不必通过这个作为参数。<​​/ p>

< 。p>在你的WinForm的类,有一个公共的委托,可以更新控制

 类的WinForm:表
{
公众委托无效updateTextBoxDelegate(字符串textBoxString); //委托类型
公共updateTextBoxDelegate updateTextBox; //委托对象

无效updateTextBox1(字符串str){textBox1.Text = STR1; } //此方法被调用

公众的WinForm()
{
...
updateTextBox =新updateTextBoxDelegate(updateTextBox1); //初始化委托对象
...
服务器SERV =新服务器();

}

从ClientConnection对象,你必须得到一个参考要在WinForm:窗体对象

 类ClientConnection 
{
...
无效显示器(字符串strItem)//可以在不同的线程从clientConnection对象
{
Form1.Invoke(Form1.updateTextBox,strItem)调用;上WINFORM
}
}



//更新TextBox1中在上述情况下, 这个不通过。


I am making a WinForms program, which requires separate threads For readability and maintainability, i have separated all non-GUI code out into different classes. This class also 'generates' another class, which does some processing. However, i have now run into the issue where i need to change a WinForms control (append a string to textbox) from a thread that was initiated in a different class

I have searched around, and found solutions for different threads, and in different classes, but not both and the solutions provided seem incompatible (to me)

This may be the biggest 'lead' however: How to update UI from another thread running in another class

Class Hierarchy example:

class WinForm : Form
{
    ...
    Server serv = new Server();
}

// Server is in a different thread to winform
class Server
{
    ...
    ClientConnection = new ClientConnection();
}

// Another new thread is created to run this class
class ClientConnection
{
    //Want to modify winform from here
}

I understand that eventhandlers are probably the way to go, but i can't work out how to do so in this situation (I am also open to other suggestions ;) )

Any help appreciated

解决方案

It does not matter from which class you are updating the form. WinForm controls have to be updated on the same thread that they were created on.

Hence, Control.Invoke, allows you to execute a method on the control on its own thread. This is also called asynchronous execution, since the call is actually queued up and executed separately.

Look at this article from msdn, the example is similar to your example. A separate class on a separate thread updates a list box on the Form.

----- Update Here you do not have to pass this as a parameter.

In your Winform class, have a public delegate that can update the controls.

class WinForm : Form
{
   public delegate void updateTextBoxDelegate(String textBoxString); // delegate type 
   public updateTextBoxDelegate updateTextBox; // delegate object

   void updateTextBox1(string str ) { textBox1.Text = str1; } // this method is invoked

   public WinForm()
   {
      ...
      updateTextBox = new updateTextBoxDelegate( updateTextBox1 ); // initialize delegate object
    ...
    Server serv = new Server();

}

From the ClientConnection Object, you do have to get a reference to the WinForm:Form object.

class ClientConnection
{
   ...
   void display( string strItem ) // can be called in a different thread from clientConnection object
   {
         Form1.Invoke( Form1.updateTextBox, strItem ); // updates textbox1 on winForm
   }
}

In the above case, 'this' is not passed.

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

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