多线程和更新GUI [英] Multithreading and updating the GUI

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

问题描述

大家好,


我有一个通过像这样的委托来表示GUI的工程:


if(deviceInsertedDelegate)deviceInsertedDelegate( stringAddress);


现在在我的Form'代码中我写这样的代码:


private delegate void DeviceInsertedCallback(string Address);


public void DeviceInserted(string Address)

{

if(this.label1.InvokeRequired)

{

调用(新的DeviceInsertedCallback(DeviceInserted),新的

对象[] {地址});

}

其他

{

label1.Text ="登录:" +地址;

}

}


现在我的问题是,是否可能没有上面的代码在我的

表格中。是否可以从我的

工作线程中同步(或其他)。


提前致谢,


Bart

Hi all,

I have a workerthread which notyfies the GUI through a delegate like this:

if (deviceInsertedDelegate) deviceInsertedDelegate(stringAddress);

Now in my Form''s code I write code like this:

private delegate void DeviceInsertedCallback(string Address);

public void DeviceInserted(string Address)
{
if (this.label1.InvokeRequired)
{
Invoke(new DeviceInsertedCallback(DeviceInserted), new
object[] { Address });
}
else
{
label1.Text = "Login : " + Address;
}
}

Now my question is if it''s possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.

Thanks in advance,

Bart

推荐答案

2008年6月24日星期二12:59:44 -0700,< bwrote:
On Tue, 24 Jun 2008 12:59:44 -0700, <bwrote:

[...]

现在我的问题是,如果我的
$ b中可能没有上述代码$ b表格。是否可以从我的

工作线程中同步(或其他)。
[...]
Now my question is if it''s possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.



是的,它是可能的,甚至没有改变你设计的API。

但恕我直言,最好不要这样做。您的表单委托方法有更多隐含的

信息,可以更恰当地管理是否

调用Invoke()。除非您的工作线程专门用于处理GUI类(后台工作类将是规则的此类异常的例子),我认为它'最好是

离开调用实际的GUI代码。


对于它的价值,你可以简化你的委托方法: br />

public void DeviceInserted(string Address)

{

Invoke((MethodInvoker)delegate {label1.Text ="登录:

+地址;});

}


你没有具体说明为什么你更喜欢没有像你发布的那样的代码

,但是你可能会发现上面的更好。


我个人不喜欢MSDN - 推荐的检查技术

InvokeRequired。在我遇到过的几乎所有例子中,该方法仅在需要调用时才使用
。这与我对一个

方法的厌恶一样,基本上做了两件完全不同的事情(调用

本身,或者实际上做了一些工作)引导我去替代

我在上面提出的实现。


即使有时在不需要调用时使用该方法,也有

否调用Invoke()时的危害。被调用的代表将直接被称为



Pete

Yes, it is possible, and without even changing the API you''ve designed.
But IMHO it''s better not to. Your form delegate method has more implicit
information and can more appropriately manage the decision as to whether
to call Invoke() or not. Unless your worker thread is specifically
designed to deal with GUI classes (the BackgroundWorker class would be an
example of this type of exception to the rule), I think it''s better to
leave the invoking to the actual GUI code.

For what it''s worth, you could simplify your delegate method though:

public void DeviceInserted(string Address)
{
Invoke((MethodInvoker)delegate { label1.Text = "Login: "
+ Address; });
}

You''re not specific about why you''d prefer not to have code like that
which you posted, but it''s possible you''d find the above preferable.

I personally dislike the MSDN-recommended technique of checking
InvokeRequired. In almost every example I''ve ever run into, the method is
used only when invoking is required. That along with my distaste for a
method that is basically doing two entirely different things (invoking
itself, or actually doing some work) leads me to the alternate
implementation I propose above.

Even if the method is sometimes used when invoking isn''t required, there''s
no harm in calling Invoke(). The invoked delegate will simply be called
directly.

Pete


b写道:
b wrote:

大家好,


我有一个通过像这样的代表对GUI进行图形处理的工具:

if(deviceInsertedDelegate)deviceInsertedDelegate(stringAddress);


现在在我的表格'代码中我写这样的代码:


private delegate void DeviceInsertedCallback(string Address);


public void DeviceInserted(string Address)

{

if(this .label1.InvokeRequired)

{

调用(新的DeviceInsertedCallback(DeviceInserted),新的

object [] {Address});

}

else

{

label1.Text ="登录:" +地址;

}

}


现在我的问题是,是否可能没有上面的代码在我的

表格中。是否可以从我的

工作线程中同步(或其他)。


提前致谢,


Bart
Hi all,

I have a workerthread which notyfies the GUI through a delegate like this:

if (deviceInsertedDelegate) deviceInsertedDelegate(stringAddress);

Now in my Form''s code I write code like this:

private delegate void DeviceInsertedCallback(string Address);

public void DeviceInserted(string Address)
{
if (this.label1.InvokeRequired)
{
Invoke(new DeviceInsertedCallback(DeviceInserted), new
object[] { Address });
}
else
{
label1.Text = "Login : " + Address;
}
}

Now my question is if it''s possible not to have the code as above in my
form. Would it be possible to synchronize (or whatever) from within my
worker thread.

Thanks in advance,

Bart



只有GUI线程可以更新控件,所以你需要以某种方式在GUI线程中运行

代码。


而不是检查是否需要调用,你可以直接调用

后台线程进行调用。你知道后台线程总是需要调用



调用方法的另一种方法是将数据放入某些

(同步或易失性)变量,两个线程都可以访问,

并在GUI中有一个计时器,用于检查变量的值以便放置

在标签上。


-

G?跑Andersson

_____
http://www.guffa.com


嗨Peter,


感谢您的快速回答
Hi Peter,

Thanks for your quick answer

对于它的价值,您可以简化您的委托方法: br />

public void DeviceInserted(string Address)

{

Invoke((MethodInvoker)delegate {label1.Text ="登录: +

地址;});

}
For what it''s worth, you could simplify your delegate method though:

public void DeviceInserted(string Address)
{
Invoke((MethodInvoker)delegate { label1.Text = "Login: " +
Address; });
}



我个人不喜欢它当我的工作线程必须知道任何关于

gui .......如果下次使用文本框怎么办.....

personally I don''t like it when my worker thread has to know anything about
the gui.......what if a textbox is used next time.....


你没有具体说明为什么你不愿意拥有这样的代码
你发布的
,但是你可能会找到上面的优选。
You''re not specific about why you''d prefer not to have code like that
which you posted, but it''s possible you''d find the above preferable.



我不想让用户使用我的代码和责任

按照我写的方式处理它。那就是为什么


还剩下什么想法?


巴特

I don''t want to saddle up the user which uses my code with the responsibilty
to handle it the way I wrote. That''s why

Any ideas left ?

Bart


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

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