跨线程操作 [英] cross-thread operation

查看:82
本文介绍了跨线程操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行客户端 - 服务器应用程序,但我遇到了这个问题:

服务器中形成调用InitializeComponent();创建一个文本框。

后来调用:

readThread = new Thread(new ThreadStart(RunServer));

readThread.Start( );

attemt更新文本框(在函数RunServer中)。

我得到一个异常,说跨线程操作无效,因为

试图访问在另一个帖子中创建的texbox。


我该怎么办?


Danny K

I am trying to make a client-server application to run, but I am
running into this problem:
in the server Form the call InitializeComponent(); creates a text-box.
Later the calls:
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();
attemt to update the textbox (in the function RunServer).
I get an exception saying that cross thread operation is invalid as
there is an attempt to access a texbox created in a nother thread.

what should I do?

Danny K

推荐答案

.. net it self它在一个线程上运行,所以你试图交叉引用

应用程序线程。


使用表单计时器进行任何UI更新.. System.Windows.Form.Timer将解决

这个问题..

在你的情况下,你可以设置一个静态变量,由RunServer更新

方法,它将由表单计时器选择,并将UI更新时间更新为

时间。


Nirosh。


" dani kotlar" < da ********* @ gmail.comwrote in message

news:11 ********************* *@c28g2000cwb.googlegr oups.com ...
..net it self it run on a thread so you are attampting to cross refer to the
application thread.

use form timer to do any UI updates.. System.Windows.Form.Timer will solve
this problem..

in ur case you may set a static variable which update by the RunServer
method where it will pick by the form timer and does the UI update time to
time.

Nirosh.

"dani kotlar" <da*********@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...

>我正在尝试运行客户端 - 服务器应用程序,但我是

遇到这个问题:
服务器中的
形成调用InitializeComponent();创建一个文本框。

后来调用:

readThread = new Thread(new ThreadStart(RunServer));

readThread.Start( );

attemt更新文本框(在函数RunServer中)。

我得到一个异常,说跨线程操作无效,因为

试图访问在另一个帖子中创建的texbox。


我该怎么办?


Danny K
>I am trying to make a client-server application to run, but I am
running into this problem:
in the server Form the call InitializeComponent(); creates a text-box.
Later the calls:
readThread = new Thread(new ThreadStart(RunServer));
readThread.Start();
attemt to update the textbox (in the function RunServer).
I get an exception saying that cross thread operation is invalid as
there is an attempt to access a texbox created in a nother thread.

what should I do?

Danny K



简单;生成的线程无法直接与TextBox对话;你需要

使用.Invoke或.BeginInvoke切换线程,并要求UI线程代表你做

...

如果您只是阅读该值,另一个选择是在*生成线程之前读取值

*,并使值可用于生成

线程。在2.0中,使用捕获的变量和匿名代表很容易:


string currentText = textbox1.Text;

线程readThread =新线程(

(ThreadStart)委托{

SomeFunctionWithParams(currentText);

});

readThread.Start();


同样,更新:


string newValue =" abc";

textbox1.BeginInvoke((MethodInvoker)delegate {

textbox1.Text = newValue;

});


请注意,BeginInvoke将并行运行,所以不要在

之后改变newValue,作为textbox1.Text = newValue; line可能还没有运行。你可以使用Invoke来串行运行,但如果UI线程

当前正在等待你的线程做某事(死锁),这可能会导致问题。但它可能不应该是



最后一个注意事项 - 对于更新你也可以采用假脱机类型的方法来支付

两个线程只是简单地与(同步的)对象/变量对话 - 并且

UI在计时器上更新自己。这可以允许您在单个UI更新中组合多个

逻辑更新,但需要一个计时器。不同的帽子

不同的头。


Marc
Simple; the spawned thread cannot talk to the TextBox directly; you need to
use .Invoke or .BeginInvoke to switch threads, and ask the UI thread to do
it on your behalf...

If you are just reading the value, another option is to read the value
*before* spawning the thread, and make the value available to the spawned
thread. In 2.0 this is easy with captured variables and anonymous delegates:

string currentText = textbox1.Text;
Thread readThread = new Thread(
(ThreadStart)delegate {
SomeFunctionWithParams(currentText);
});
readThread.Start();

Similarly, for updating:

string newValue = "abc";
textbox1.BeginInvoke((MethodInvoker) delegate {
textbox1.Text = newValue;
});

Note that BeginInvoke will run in parallel, so don''t change newValue after
this, as the "textbox1.Text = newValue;" line might not have run yet. You
can use Invoke to run in series, but this can cause issues if the UI thread
is currently waiting for your thread to do something (deadlock). But it
probably shouldn''t be anyway.

A final note - for updates you could also have a spooler type approach with
the two threads simply talking to (synchronised) objects / variables - and
the UI updating itself on a timer. This can allow you to combine multiple
logical updates in a single UI update, but requires a timer. Different hats
for different heads.

Marc


...你可以设置静态变量...


我不推荐静态 OP ...没有提到表单

是一个单例,所以当多个表格

实例打开时,使用static可能会引入错误。作为一般规则,尽可能保持与

紧密相关的范围;这些变量与表单的实例有关,因此如果使用基于计时器的
方法(参见其他文章),则使用表单级(实例)字段

/物业。


Marc
...you may set a static variable...

I wouldn''t recommend "static" here... the OP didn''t mention that the form
was a singleton, so using static could introduce bugs when multiple form
instances are open. As a general rule, keep things as tightly scoped as
possible; these variables relate to an instance of a form, so if using a
timer-based approach (see other post), then use form-level (instance) fields
/ properties.

Marc


这篇关于跨线程操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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