2012-05-13 135 views
2

我想在我的形式获得从BackgroundWorker一个控件的属性获取一个控件的属性:从另一个线程

foreach (ListViewItem i in ListView.CheckedItems) { //error: Cross-thread operation not valid: Control 'ListView' accessed from a thread other than the thread it was created on. 
    //do something with i 
} 

任何人都可以提出来做到这一点的最简单,最简单的方法是什么?

+0

可能的重复:http://stackoverflow.com/questions/6092519/winforms-thread-safe-control-access – zmbq

+0

如果您可以使用'BackgroundWorker',您表单中的每个控件都已经可读... – Marco

+0

向我们展示您正在使用的代码请... – Marco

回答

2

让我带这另一个刺...

1)拖动一个ListView到窗体

2)一个BackgroundWorker拖到窗体

3)创建的方法不要通过ListViewItem集合

private void LoopThroughListItems() 
{ 
    foreach (ListViewItem i in listView1.CheckedItems) 
     DoSomething(); 

} 

4)添加代码来调用LoopThroughListItems()BackgroundWorker的的DoWork的事件

0123内循环
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
{ 
    LoopThroughListItems(); 
} 

5)在你的窗体加载 - 执行主线程代码(它的工作原理),那么backgroundWorkder线程(失败)

private void Form1_Load(object sender, EventArgs e) 
{ 
    // Try it on the UI Thread - It works 
    LoopThroughListItems(); 

    // Try it on a Background Thread - It fails 
    backgroundWorker1.RunWorkerAsync(); 

} 

6)修改代码以使用IsInvokeRequired /调用

private void LoopThroughListItems() 
{ 

    // InvokeRequired == True when executed by non-UI thread 
    if (listView1.InvokeRequired) 
    { 
     // This will re-call LoopThroughListItems - on the UI Thread 
     listView1.Invoke(new Action(LoopThroughListItems)); 
     return; 
    } 

    foreach (ListViewItem i in listView1.CheckedItems) 
     DoSomething(); 
} 

7.)再次运行应用程序 - 现在它在UI线程和非UI线程上工作。

这解决问题。检查IsInvokeRequired /调用是你习惯了很多常见的模式(这就是为什么它是包含在所有控件)。如果你正在做的所有的地方,你可以做的很漂亮,敷了这一切 - 这里描述:Automating the InvokeRequired code pattern

1

尝试是这样的:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void OnClick(object sender, EventArgs e) 
     { 
      backgroundWorker1.RunWorkerAsync(); 
     } 

     private void OnDoWork(object sender, DoWorkEventArgs e) 
     { 
      foreach (ListViewItem i in GetItems(listView1)) 
      { 
       DoSomething(i); 
      } 
     } 

     private IEnumerable<ListViewItem> GetItems(ListView listView) 
     { 
      if (InvokeRequired) 
      { 
       var func = new Func<ListView, IEnumerable<ListViewItem>>(GetItems); 
       return (IEnumerable<ListViewItem>)Invoke(func, new[] { listView }); 
      } 
      // Create a defensive copy to avoid iterating outsite UI thread 
      return listView.CheckedItems.OfType<ListViewItem>().ToList(); 
     } 

     private void DoSomething(ListViewItem item) 
     { 
      if (InvokeRequired) 
      { 
       var action = new Action<ListViewItem>(DoSomething); 
       Invoke(action, new[] { item }); 
       return; 
      } 
      // Do whatever you want with i 
      item.Checked = false; 
     } 
    } 
} 

然而,你的问题真的很一般。如果你分享更多的细节,也许会有一个更容易或更好的解决方案。