2012-04-28 67 views
-1

我有這樣的功能:獲取項[I]從使用線程一個ListView

foreach (ListViewItem item in getListViewItems(listView2)) //for proxy 
{ 
    if (reader.Peek() == -1) 
    { 
     break; 
    } 

    lock (reader) 
    { 
     line = reader.ReadLine(); 
    } 


    //proxy code 
    List<string> mylist = new List<string>(); 
    if (item != null) 
    { 
     for (int s = 0; s < 3; s++) 
     { 
      if (item.SubItems[s].Text != null) 
      { 
       mylist.Add(item.SubItems[s].Text); 
      } 
      else 
      { 
       mylist.Add(""); 
      } 
     } 
    } 
    else 
    { 
     break; 
    } 
    //end proxy code 


    //some other code including the threadpool 
} 

和委託代碼:

private delegate ListView.ListViewItemCollection GetItems(ListView lstview); 
private ListView.ListViewItemCollection getListViewItems(ListView lstview) 
{ 
    ListView.ListViewItemCollection temp = new ListView.ListViewItemCollection(new ListView()); 
    if (!lstview.InvokeRequired) 
    { 
     foreach (ListViewItem item in lstview.CheckedItems) 
     { 
      temp.Add((ListViewItem)item.Clone()); 
     } 
     return temp; 
    } 
    else 
    { 
     return (ListView.ListViewItemCollection)this.Invoke(new GetItems(getListViewItems), new object[] { lstview }); 
    } 
} 

編輯:

我想替換的foreach循環在主函數條件函數:

if (reader.Peek() == -1) 
{ 
    break; 
} 

lock (reader) 
{ 
    line = reader.ReadLine(); 
} 


if (use_proxy == true) 
{ 
    mylist2 = get_current_proxy(); 
} 

//some other code including the threadpool 

private List<string> get_current_proxy() 
{ 
    //what shall I add here? 
} 

如何使該函數與foreach循環相同,但使用for循環?我的意思是讓一個在代理一個...

回答

1

我看到周圍刮網站的電子郵件,然後發送垃圾郵件的想法旋轉多個問題。你已經有非常酷的工具,不需要新的工具。

反正 - 我不明白你的問題,它似乎是我在這裏不是唯一的一個,但事情你必須要知道之前別的是:

有任何東西在Windows在多線程運行將最終必須同步,當你做Invoke()這不得不等待,直到它都經過一個線程,就是這樣擁有消息循環的一個。所以,你可以嘗試讀取或從多個線程寫入ListView,但要做到每個讀/寫你必須Invoke()(你可能直接嘗試過了,BAAAAM)和每一個調用()只有一個孔穿過去,而你所有的線程都必須等待輪到他們。

下一頁:具有ListView控件來爲您的數據的容器是如此糟糕,我甚至不能進一步置評任。考慮的東西作爲

class MyData 
{ 
    public string Name; 
    public string URL; 
    // ... 
} 

List<MyData> _myData; 

來保存數據。如果您關心一些低調的同步問題,您將能夠從多個線程訪問它。

最後,你怎麼來問我們有關.NET C#編程的問題,如果你不知道的語法。恩,這是修辭,...