2012-05-13 130 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; 
     } 
    } 
} 

然而,你的問題真的很一般。如果你分享更多的細節,也許會有一個更容易或更好的解決方案。