Winforms和线程 [英] Winforms and threads

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

问题描述

我知道Winform上的控件不是线程安全的,并且你想要

来在一个线程上完成所有UI更新 - 通常是主要的

线程。


现在,2个问题:

1.你可以用来更新控件的一个线程是否必须是
你在实例化表单时遇到的
线程?


2.同一个应用程序中的2个表单对不同的线程有亲和力,

或者您是否希望在主要的

线程中创建应用程序中的所有表单?


为什么我要问:


我正在研究一个将物品放在系统托盘中的模块。与托盘项关联的代码

将使用辅助线程来监听

系统事件,并且基于某些事件,它可能必须启动

表格。我是否需要在某些逻辑中工作以确保表单启动

代码始终由主线程执行?

I know that the controls on a Winform are not thread safe, and that you want
to do all of your UI updates on a single thread -- generally the main
thread.

Now, 2 questions:
1. Does the one thread that you can use to update controls have to be the
thread that you were on when you instanced the form?

2. Can 2 forms in the same application have affinity for different threads,
or do you want all of the forms in an application to be created on the main
thread?

Why I''m asking:

I''m working on a module that will place an item in system tray. The code
associated with the tray item will use a secondary thread to listen for
system events, and based on some of the events, it might have to launch a
form. Do I need to work in some logic to make sure that the form launch
code is always executed by the main thread?

推荐答案

你好J.Marsch,
Hi J.Marsch,
我知道Winform上的控件不是线程安全的,而你
想要在a上做所有的UI更新单线程 - 一般是主要的线程。
现在,2个问题:
1.你可以用来更新控件的一个线程是否必须是你的线程?当你填写表格时,他们还在吗?


创建控件的线程(此线程拥有底层

windows''控件句柄)可以更新控件。这是Windows要求

而不是Windows Form的一个。

2.同一个应用程序中的2个表单是否可以与不同的
线程具有亲和力,或者是您希望在
主线程上创建应用程序中的所有表单吗?
您可能有许多UI线程,其中任何一个都可能创建了一堆表单和

控件。在Windows平台上运行的.NET版本的规则是:

您必须仅从拥有控件'

HWND的线程更新控件(这个是线程创建控件)

为什么我要问:

我正在研究一个将项目放在系统托盘中的模块。与托盘项目相关联的代码将使用辅助线程来监听系统事件,并且基于某些事件,它可能必须启动一个
表单。我是否需要在某些逻辑中工作以确保表单启动代码始终由主线程执行?
I know that the controls on a Winform are not thread safe, and that you want to do all of your UI updates on a single thread -- generally the main
thread.
Now, 2 questions:
1. Does the one thread that you can use to update controls have to be the
thread that you were on when you instanced the form?
The thread which has created the control (this thread owns the underlying
windows'' control handle) can update the control. This is Windows requirement
rather than Windows Form''s one.
2. Can 2 forms in the same application have affinity for different threads, or do you want all of the forms in an application to be created on the main thread? You may have many UI threads any of them may has created bunch of forms and
controls. The rule for the version of .NET running on windows platform is:
You have to update a control only from the thread that owns the control''s
HWND (this is thread created the control)
Why I''m asking:

I''m working on a module that will place an item in system tray. The code
associated with the tray item will use a secondary thread to listen for
system events, and based on some of the events, it might have to launch a
form. Do I need to work in some logic to make sure that the form launch
code is always executed by the main thread?



来自您可以调用的任何线程Control.Invoke在UI中执行一个方法

线程创建了控件或表单,让这个方法更新控件。

通常使用主窗体'的Invoke。


HTH

B \ rgds



From any thread you can call Control.Invoke to execute a method in the UI
thread created the control or form and let this method udate the controls.
Usually main form''s Invoke is used.

HTH
B\rgds
100


你好,


文章< un ************** @ TK2MSFTNGP09.phx.gbl>,
je **** @ ctcdeveloper.com 说...
我知道Winform上的控件不是线程安全的,并且您希望
在单个线程上完成所有UI更新 - 通常是主要的线程。

现在,2个问题:
1.是否您可以用来更新控件的一个线程必须是您在实例化表单时所使用的线程吗?

2。可以在同一个应用程序中的2个表单对不同的线程有亲和力,或者您是否希望在主要的线程上创建应用程序中的所有表单?

为什么我我问:

我正在研究一个将物品放在系统托盘中的模块。与托盘项目相关联的代码将使用辅助线程来监听系统事件,并且基于某些事件,它可能必须启动一个
表单。我是否需要在某些逻辑中工作以确保表单启动代码始终由主线程执行?
I know that the controls on a Winform are not thread safe, and that you want
to do all of your UI updates on a single thread -- generally the main
thread.

Now, 2 questions:
1. Does the one thread that you can use to update controls have to be the
thread that you were on when you instanced the form?

2. Can 2 forms in the same application have affinity for different threads,
or do you want all of the forms in an application to be created on the main
thread?

Why I''m asking:

I''m working on a module that will place an item in system tray. The code
associated with the tray item will use a secondary thread to listen for
system events, and based on some of the events, it might have to launch a
form. Do I need to work in some logic to make sure that the form launch
code is always executed by the main thread?




每个表单有Form.InvokeRequired,你必须检查,看看是否需要调用



也许这不是一个好的解决方案,但在我的形式(我想改变) a

标签字段)我使用这些新增内容:


私有委托void ChangeActionTextDelegate(字符串newtext);


public void SetActionText(string txt)

{

if(this.InvokeRequired)

this.Invoke(new ChangeActionTextDelegate(

this.ChangeActionText),new object [] {txt});


else

this.ChangeActionText(txt);

}


private void ChangeActionText(string newtext)

{

this.lAction.Text = newtext;

this.lAction.Refresh();

}


所以,每当你想改变文本时,都没关系从哪个

线程,你调用公共方法SetActionText,并确保

调用在必要时被转发到表单线程。

此外,似乎公共方法看起来像:


public void SetActionText(string txt)

{

if(this.InvokeRequired)

this.Invoke(new ChangeActionTextDelegate(

this.SetActionText),new object [] {txt});


else

{

this.lAction.Text = newtext;

this.lAction.Refresh();

}

}


即,你可以跳过私有方法,并使表单线程为

调用相同的公共方法。


我不喜欢这个,因为我不太确定它是否安全

(我尝试过,它有效),因为在第一种方法中,事物是更可读的。


所以,你可以使用类似的东西对于每个表单控件,whic你需要改变




!!!小心!!!


我不知道为什么,但这种方法不适用于属性。如果

你将if语句放在公共属性中,而不是放在方法中,它就会以相同的方式挂起,就像你试图直接更改控件一样。


希望有所帮助

Sunny



Every form has Form.InvokeRequired, which you have to inspect to see if
you should need Invoke.
Maybe this is not good solution, but in my form (I want to change a
label field) I use this additions:

private delegate void ChangeActionTextDelegate(string newtext);

public void SetActionText(string txt)
{
if (this.InvokeRequired)
this.Invoke(new ChangeActionTextDelegate(
this.ChangeActionText),new object[] {txt});

else
this.ChangeActionText(txt);
}

private void ChangeActionText(string newtext)
{
this.lAction.Text = newtext;
this.lAction.Refresh();
}

So, whenever you want to change the text, doesn''t matter from which
thread, you call the public method SetActionText, and it makes sure that
the call is forwarded to the form thread if necessary.

Also, it seems possible the public method to look like:

public void SetActionText (string txt)
{
if (this.InvokeRequired)
this.Invoke(new ChangeActionTextDelegate(
this.SetActionText), new object[] {txt});

else
{
this.lAction.Text = newtext;
this.lAction.Refresh();
}
}

I.e, you may skip the private method, and to make the form thread to
invoke the same public method.

I just don''t like this, becausee I''m not so sure that it is safe at all
(I tried, it worked), and because in the first approach the things are
more readable.

So, you can use similar things for every form control, which you have to
change.

!!!CAUTION!!!

I do not know why, but this approach does not work with properties. If
you put the if statement in a public property, and not in a method, it
hangs the same way, as if you are trying to change a control directly.

Hope that helps
Sunny




你好,


在winform中,Windows窗体中的控件绑定到特定的线程并且

不是线程安全的。

有四种方法在一个可以安全地从任何线程调用的控件上:

Invoke,BeginInvoke,EndInvoke和CreateGraphics。对于所有其他方法

调用,你应该在从

不同的线程调用时使用这些调用方法之一。


下面的示例告诉您如何操作多线程winform

控件:
http://msdn.microsoft.com/library/de...us/cpguide/htm

l / cpconDevelopingMultithreadedWindowsFormsControl.as p


希望这会有所帮助,

祝你好运,

Jeffrey Tan

Microsoft在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。


Hi,

In winform, controls in Windows Forms are bound to a specific thread and
are not thread safe.
There are four methods on a control that are safe to call from any thread:
Invoke, BeginInvoke, EndInvoke and CreateGraphics. For all other method
calls, you should use one of these invoke methods when calling from a
different thread.

The sample below tells you how to manipulate multithreading winform
controls:
http://msdn.microsoft.com/library/de...us/cpguide/htm
l/cpconDevelopingMultithreadedWindowsFormsControl.as p

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


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

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