以一种形式创建并以另一种形式进入的线程. [英] Thread created in one form and accesed in another form.

查看:116
本文介绍了以一种形式创建并以另一种形式进入的线程.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以form2编写的代码:-

code written in form2:-

private void Form2_Load(object sender, EventArgs e)
       {
           for (int i = 0; i < 1000; i++)
           {
               str=i.ToString();
               object o = (object)str;
               listBox1.Items.Add(i.ToString());
               Thread t = new Thread(new ParameterizedThreadStart(Form1.MyMethod));
               t.Start(o);
           }
       }


以form1编写的代码:-


code written in form1:-

public static void MyMethod(object o)
       {
           string str = (string)o;
           listBox1.Items.Add(str);
       }


我如何并行运行这两种形式并以两种形式并行显示i值.


how can i run these two forms in parallel and display the i values paralelly in two forms.

推荐答案

我只需要解释为什么这个问题没有意义完全.线程本身并不是真正的访问"对象.但是线程可以访问多个线程共享的一些对象;这可能是个问题. (我不认为琐碎的方面如访问线程对象以更改线程优先级或其他属性,启动或中止线程等;这与线程的逻辑行为本质上无关.)

现在,不同形式的问题与穿线完全正交.即使一个人可以运行一个以上的Application实例是一个以上的独立UI线程(我测试了这种变态情况!),也可以认为这是一种病理情况.在正常情况下,将仅在一个UI线程中创建和使用应用程序的所有表单和其他控件.这是与运行应用程序入口点的线程相同的线程(通常是Main方法).

对于线程,访问哪种控件或形式没有区别:如果这不是UI线程,则它根本无法访问任何IO对象.

方法完全不同:所有UI对象都仅在UI线程中创建和使用,但是其他线程可以通过将它们放置在队列中来将任何委托实例与调用所需的所有参数序列化. UI对象的适当方法和属性的实际调用始终由UI线程完成.

而不是从非UI线程调用任何UI方法或属性,您需要使用System.Windows.Threading.Dispatcher的方法InvokeBeginInvoke(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms). />
在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

现在,我建议您进行其他修复:不要使用ParametrizedThreadStart:它需要类型大小写,这很糟糕,而且绝对不需要.相反,线程启动方法可以是实例方法(非静态),因此可以使用通过"this"参数对其声明类的访问.有关更多详细信息,请参阅我过去的回答中提供的线程包装器:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

—SA
I just need to explain why the question makes no sense at all. A thread itself is not really "accessed"; but the thread can access some objects shared by more then one thread; and this can be a problem. (I do not count such trivial aspect as accessing the thread object in order to change thread priority or other properties, start or abort thread, etc.; this is not essentially related to thread logical behavior.)

Now, the problem of different forms is completely orthogonal to the threading. Even though one can run more than one instance of Application is more than one separate UI thread (I tested such perverted case!), this can be considered as a pathological case. In normal situation, all forms and other controls of the application are created and used in only one UI thread; this is the same thread that the one running the entry point of application (normally, Main method).

For a thread, there is no difference what control or form to access: if this is not a UI thread, it cannot access any IO objects at all.

The approach is totally different: all UI objects are created and use in the UI thread only, but other thread can serialize any delegate instance with all parameters needed for the call by putting them in a queue. The actual call of appropriate methods and properties of the UI object is always done by the UI thread.

Instead of calling any UI methods or properties from a non-UI thread, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Now, I would advice additional fix: do not use ParametrizedThreadStart: it requires type case, which is bad, and is absolutely not needed. Instead, a thread start method can be an instance method (non-static) and hence, use the access to its declaring class through "this" parameter. For further detail, please see my thread wrapper I offered in my past answers:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

—SA


这篇关于以一种形式创建并以另一种形式进入的线程.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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