BeginInvoke和匿名委托 [英] BeginInvoke and anonymous delegate

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

问题描述

void DisplayLines(对象状态)

{

for(int i = 0; i< 500; ++ i)

{

int iCopy = i;

rtb.BeginInvoke((MethodInvoker)代表

{

rtb.AppendText (iCopy +" \ n");

});

}

MessageBox.Show(" Done"); < br $>
}


我使用ThreadPool的

QueueUserWorkItem方法从主线程调用此函数。因为我从

访问RichTextBox变量rtb,所以我创建的线程不同于我必须调用

BeginInvoke方法。

以上代码似乎工作正常,但我不确定它是否能保证,因为

iCopy,从代理中访问的变量,可能

在调用委托之前超出范围。这是否意味着iCopy可以在委托访问之前销毁

void DisplayLines(object state)
{
for (int i = 0; i < 500; ++i)
{
int iCopy = i;
rtb.BeginInvoke((MethodInvoker)delegate
{
rtb.AppendText(iCopy + "\n");
});
}
MessageBox.Show("Done");
}

I call this function from the main thread with ThreadPool''s
QueueUserWorkItem method. Since I''m accessing RichTextBox variable rtb from
the different thread than the one it was created on I have to call
BeginInvoke method.

Above code seems to work fine but I''m not sure if it''s guaranteed to since
iCopy, the variable that is accessed from within the delegate, potentially
goes out of scope before delegate is invoked. Does this mean that iCopy can
be destroyed prior to delegate accessing it?

推荐答案

你好,Dean!

你写的是2006年9月3日星期日03:25:48 +0200:


DSAbove代码似乎工作正常但我不确定它是否''保证



DSiCopy,从代表内部访问的变量,

可能

DSgoes在调用委托之前超出范围。这是否意味着iCopy

可以

DSbe在委托访问之前销毁?


iCopy将成为隐藏的成员上课

查看匿名代表如何工作的详细信息

http://blogs.msdn.com/oldnewthing/ar...02/686456.aspx


-

问候,Vadym Stetsyak。

博客: http://vadmyst.blogspot.com
Hello, Dean!
You wrote on Sun, 3 Sep 2006 03:25:48 +0200:

DSAbove code seems to work fine but I''m not sure if it''s guaranteed to
since
DSiCopy, the variable that is accessed from within the delegate,
potentially
DSgoes out of scope before delegate is invoked. Does this mean that iCopy
can
DSbe destroyed prior to delegate accessing it?

iCopy will become a member of the hidden class
Have a look at for more details how anonymous delegates work
( http://blogs.msdn.com/oldnewthing/ar...02/686456.aspx )

--
Regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com


更好的解决方案是在后台线程上构建字符串

(理想情况下使用StringBuilder实例),然后对UI线程执行* single *

BeginInvoke tortb.AppendText(sb.ToString()); 。这个

是一个:更有效率(更少的消息发布等),但更重要的是,

b:它更简单:你不需要担心生命周期所以很多。

哦,而且c:它会导致更新的UI更少,使UI线程更有效,更平滑......但是UI会在单个内容中更新跳。如果

这是一个问题,你可以随时使用.Invoke,例如

i%100为零(然后一次结束) - .Invoke避免任何

从两个线程访问捕获变量的问题,而你

仍然看到逐渐增长的UI ...


Marc

A better solution is to build the string on the background thread
(ideally using a StringBuilder instance), and then perform a *single*
BeginInvoke to the UI thread to "rtb.AppendText(sb.ToString());". This
is a: more efficient (less message posting etc), but more importantly,
b: it is simpler: you don''t need to worry about the lifetime so much.
Oh, and c: it will cause less UI updates, making the UI thread more
efficient, and smoother... but the UI will update in a single jump. If
this is a problem, you could always to a .Invoke whenever for instance
i % 100 is zero (and then once at the end) - the .Invoke avoids any
question of accessing the captured variable from two threads, and you
still see a progressively growing UI...

Marc


On Sun,2006年9月3日20:48:27 +0300,Vadym Stetsyak写道:
On Sun, 3 Sep 2006 20:48:27 +0300, Vadym Stetsyak wrote:

你好,Dean!

你写的是Sun,2006年9月3日03:25:48 +0200:


DSAbove代码似乎工作正常但是我不确定它是否保证



DSiCopy,从代表内部访问的变量,

可能

在调用委托之前,DS会超出范围。这是否意味着iCopy

可以

DSbe在委托访问之前销毁?


iCopy将成为隐藏的成员上课

查看匿名代表如何工作的详细信息

http://blogs.msdn.com/oldnewthing/ar...02/686456.aspx



谢谢。

Thanks.


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

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