多线程中如何invoke获得控件的属性值,紧急求解!

wangkuang5 2009-03-18 04:39:56

private void Form1_Load(object sender, EventArgs e)
{
objectListView1.Items.Clear();
objectListView1.Items.Add("1");
objectListView1.Items.Add("2");
objectListView1.Items.Add("3");
objectListView1.Items.Add("4");
objectListView1.Items.Add("5");
}
private void button1_Click(object sender, EventArgs e)
{

Thread sd = new Thread(new ThreadStart(CountItems));
sd.Start();
}

List<string> checkedselectedItems = new List<string>();

private void CountItems() {
ISynchronizeInvoke sync = objectListView1;
if (sync.InvokeRequired) {
//how to do the invoke method,get Checked Objects Items ?????
//if user checked some items in objectListView1
//I want to do nothing before objectListView1.SetObjects ,
// if do objectListView1.SetObjects and that objectListView1.CheckedObjects.Count > 0 ,
//

//bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
// {
// hasChecked = true;// to Waiting
// }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
/*
我的意思就是在这里,如果要刷新、更新ListView界面的列表值。而界面已有用户选中项目,为了不让刷新、更新界面冲掉选中项(不使选中项失效)
所以让刷新、更新界面在循环中等待,直到用户没有选中
*/


} else {

checkedselectedItems = objectListView1.CheckedObjects as List<string>;// objectListView1.SelectedObjects;

}
}



}
...全文
175 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangkuang5 2009-03-19
  • 打赏
  • 举报
回复


void Updata(IList<objectItems> ){
//bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
//objectListView1.SelectedObjects<----就是这里怎么在线程里取得属性返回值,它相当与ListView.Items属性,
//问题在这里,注意我说取值,不是赋值,赋值我知道怎么Invoke或MethodInvoker
// {
// hasChecked = true;// 等待用户操作完成 // }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
//用户操作完成了,用委托更新节目数据




}





//再举例说简单点:
//在线程里ArrayList objectItems=ListView.Items,这是我要做的事
//谁做的给谁分,不要告诉我


以下invoke形式我不坚决不给分
ListView.Items=。。。。
ListView.Items.Add(....)
wangkuang5 2009-03-19
  • 打赏
  • 举报
回复
我再说清楚一点

事实上我是在套接字接收的线程里做事,
到程序收到数据databuffer,将databuffer分析后变成objectListView1上的一行对象objectItem(它相当于ArrayList),
这时我就要更新objectListView1列表的数据,但是如果用户此时也选定了某些objectItem正准备其他操作,
如果更新objectListView1刷新界面,用户选定的那些objectItems会被冲掉,而没有选中了

所以我就想在


void Updata(IList<objectItems> ){
//bool hasChecked = false;
//while (!hasChecked)
//{
// if (objectListView1.SelectedObjects.Count == 0)
//objectListView1.SelectedObjects<----就是这里怎么在线程里取得属性返回值,它相当与ListView.Items属性
// {
// hasChecked = true;// 等待用户操作完成 // }
// else
// {
// Application.DoEvents();//to do other things
// continue;
// }
//}
//




}
sxmonsy 2009-03-19
  • 打赏
  • 举报
回复
帮顶接分
阿达King哥 2009-03-18
  • 打赏
  • 举报
回复
一般通过回调函数完成对控件的操作。
ChrisAK 2009-03-18
  • 打赏
  • 举报
回复
这个...
Lambda表达式或者匿名函数是最好的了.
没用过那个控件,写不了代码了,给个以前写的:

//跨线程添加listitem
//这里的一些操作生成了一个string[] list.
//习惯用a了- -

MethodInvoker a = ()=>{
foreach (var s in list)
{
listbox.Items.Add (s);
}
};

if (listbox.InvokeRequired)
listbox.Invoke (a);
else
a();


a里的内容可以换成对属性的访问修改等等任何你希望在UI线程里完成的事.
jlj84237485 2009-03-18
  • 打赏
  • 举报
回复
帮顶一下
oyljerry 2009-03-18
  • 打赏
  • 举报
回复
注释的代码不就是可以访问? objectListView1线程中访问的时候是否是正确的控件
wangkuang5 2009-03-18
  • 打赏
  • 举报
回复
ObjectListView----------------------->http://objectlistview.sourceforge.net/
cppfaq 2009-03-18
  • 打赏
  • 举报
回复
objectListView1是怎么东西啊?
ListView么有CheckedObjects 属性啊。

没看出来你这个线程开出来是干嘛的。

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧