foreach listviewitem在后台线程中 [英] foreach listviewitem in a background thread

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

问题描述

如何从后台线程访问我的表单上的listview控件的items集合?


我知道我需要委托给更新listview控件,我在foreach循环中有那些

调用,但我不知道如何访问这些项目

集合。


foreach(this.listview1.Items中的ListViewItem li)

{

调用(新委托(updatectrl),新对象[] {param}) ;

}


谢谢!

How can I have access to the items collection of a listview control on
my form from a background thread?

I know I need delegates to update the listview control and I have those
calls in the foreach loop, but I''m not sure how to access the items
collection.

foreach (ListViewItem li in this.listview1.Items)
{
Invoke(new deleg(updatectrl), new object [] { param });
}

Thanks!

推荐答案

5月15日星期二2007 14:53:01 -0500,deciacco< a @ awrote:
On Tue, 15 May 2007 14:53:01 -0500, deciacco <a@awrote:

>如何在我的表单来自后台线程?

我知道我需要代理更新listview控件,我在foreach循环中有那些
调用,但我不知道怎么做访问项目
集合。

foreach(ListViewItem li in t his.listview1.Items)
$

调用(新代理(updatectrl),新对象[] {param});
}

谢谢!
>How can I have access to the items collection of a listview control on
my form from a background thread?

I know I need delegates to update the listview control and I have those
calls in the foreach loop, but I''m not sure how to access the items
collection.

foreach (ListViewItem li in this.listview1.Items)
{
Invoke(new deleg(updatectrl), new object [] { param });
}

Thanks!



我的头脑:你执行一个方法Safemethod,如果它是从

调用另一个线程InvokeRequired将是真的和de方法将再次调用
,但这次是在UI线程内,并且foreach

循环可以安全使用。

private void SafeMethod ()

{

if(this.InvokeRequired)

this.Invoke(new SafeMethodDelegate(SafeMethod));

else

{

foreach(ListViewItem li in this.listview1.Items)

{

//和我一起工作

}

}

}


私人代表无效SafeMethodDelegate();


-

Ludwig
http://www.coders-lab.be


我不认为这对我有用,因为我不喜欢不希望foreach在主线程上执行
。我需要foreach保留它自己的

线程,并且foreach内部调用了线程安全的方法。

问题是我需要工作线程获得有关

列表框的信息,以便它可以将信息传递给线程安全函数来做

他们的工作。


例如,对于列表中的每个项目,我想要一个工人

线程调用setImageForListViewItem并传入当前item

索引和用于该项目的图像索引。


foreach(ListViewItem li in this.listviewProgress.Items)

{

调用(new setImageForListViewItemDelg(setImageForListViewIte m),new

object [] {2,li.Index});

}


谢谢。


Ludwig写道:
I don''t think this will work for me, because I don''t want the foreach to
be executed on the main thread. I need the foreach to stay in it''s own
thread and inside the foreach have calls to thread-safe methods. The
problem is that I need the worker thread to have information about the
listbox so it can pass on information to the thread safe functions to do
their jobs.

For example, for each one of the items in the list I want a worker
thread to call setImageForListViewItem and pass in the current item
index and the index of the image to use for that item.

foreach (ListViewItem li in this.listviewProgress.Items)
{
Invoke(new setImageForListViewItemDelg(setImageForListViewIte m), new
object[] { 2, li.Index });
}

Thanks.

Ludwig wrote:

星期二,15 2007年5月14:53:01 -0500,deciacco< a @ awrote:
On Tue, 15 May 2007 14:53:01 -0500, deciacco <a@awrote:

>我怎样才能访问
我知道我需要d elegates更新listview控件,我在foreach循环中有那些
调用,但我不知道如何访问项目
集合。

foreach(ListViewItem li在this.listview1.Items)
{
调用(新的委托(updatectrl),新对象[] {param});
}
谢谢!
>How can I have access to the items collection of a listview control on
my form from a background thread?

I know I need delegates to update the listview control and I have those
calls in the foreach loop, but I''m not sure how to access the items
collection.

foreach (ListViewItem li in this.listview1.Items)
{
Invoke(new deleg(updatectrl), new object [] { param });
}

Thanks!



我的头脑:你执行一个方法Safemethod,如果它是从

调用另一个线程InvokeRequired将是真的和de方法将再次调用
,但这次是在UI线程中,并且foreach

循环可以安全使用。


private void SafeMethod()

{

if(this.InvokeRequired)

this.Invoke(new SafeMethodDelegate(SafeMethod));

其他

{

foreach(ListViewItem li in this.listview1.Items)

{

//与......一起做

}

}

}


私人代表无效SafeMethodDelegate();


out of my head: you execute a method Safemethod, if it''s called from
another thread InvokeRequired will be true and de method will be
called again but this time from within the UI thread, and the foreach
loop can be used safely.
private void SafeMethod ( )
{
if ( this.InvokeRequired )
this.Invoke( new SafeMethodDelegate ( SafeMethod ) );
else
{
foreach (ListViewItem li in this.listview1.Items)
{
// do sth with li
}
}
}

private delegate void SafeMethodDelegate ( );



2007年5月15日星期二14:40:44 -0700,deciacco< a @ awrote:
On Tue, 15 May 2007 14:40:44 -0700, deciacco <a@awrote:

我不认为这对我有用,因为我不想要foreach来

b e在主线程上执行。我需要foreach保留它自己的

线程,并且foreach内部调用了线程安全的方法。

问题是我需要工作线程获得有关

列表框的信息,以便它可以将信息传递给线程安全函数来做

他们的工作。
I don''t think this will work for me, because I don''t want the foreach to
be executed on the main thread. I need the foreach to stay in it''s own
thread and inside the foreach have calls to thread-safe methods. The
problem is that I need the worker thread to have information about the
listbox so it can pass on information to the thread safe functions to do
their jobs.



你能澄清一下目标是什么给你带来麻烦吗?


我看到两个可能的问题,至少。一个是同步访问

Items集合。另一个是确保你的委托在每个项目的ListView控件上调用

。但是据我所知,你不是在线程运行时试图修改集合,而你*做* show

我们调用Invoke in你的例子,我真的不明白这个问题是什么?
是。 (Invoke似乎是在拥有控件上,可能是一个Form,但

无论如何都应该让你进入正确的线程。)


到底是什么你希望工人线程做到这一点你无法做b $ b,为什么你不能做到(这个问题的第二部分

和第一个恕我直言一样重要吗?


Pete

Can you clarify what about the goal is giving you trouble?

I see two possible issues, at least. One is synchronizing access to the
Items collection. The other is making sure your delegate gets invoked on
the ListView control for each item. But since as far as I know you''re not
trying to modify the collection while the thread runs, and you *do* show
us a call to Invoke in your example, I don''t really see what the problem
is. (The Invoke appears to be on the owning control, probably a Form, but
that should put you on the correct thread anyway).

What exactly is it you want the worker thread to do that you''re not able
to do, and why aren''t you able to do it (the second part of that question
is as important as the first IMHO)?

Pete


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

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