Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C# returning data using delegates...

Options
  • 16-02-2006 9:47pm
    #1
    Closed Accounts Posts: 4,943 ✭✭✭


    I'm having a bit of trouble with my delegates. I'm trying to return all the items in a listview control in a winform in order to use their data to download files off the internet in a seperate "downloading" thread.

    If i try:
    foreach(ListViewItem item in this.MyListView.Items)
    {
    }

    directly in my downloader thread, an illegal thread access exception thingy is thrown... How do i use delegates to return me a nice ListViewItemCollection in order to iterate through them in my foreach loop in my "download" thread?

    Or is there a better way of doing this?


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    If you remove "this"?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    testing that now... but i don't see how it'd help.

    EDIT: Removed the "this." but it didn't help. Still getting the illegal cross-thread exception.


  • Closed Accounts Posts: 223 ✭✭Chris P Duck


    Try this
    int itemsCount = MyListView.Items.Count;
    for(int index = 0; index < itemsCount; index++)
    {
        ListViewItem.ListViewSubItemCollection subCollection =
          new System.Windows.Forms.ListViewItem.ListViewSubItemCollection(MyListView.Items[index]);
        subCollection = MyListView.Items[index].SubItems;
    }
    


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Thing is, even running:

    int itemsCount = MyListView.Items.Count;

    is calling a method, which ain't allowed unless i'm running the method from the same thread the listview is on, which i'm not doing! There has to be an easy way of doing this by using a delegate with return type ListViewItemCollection, but i can only make delegates with void return work :p


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Figured it out. Here's what i ended up doing (just in case anyone else ever needs to do it... its also what i tried first, but it didn't work for me then because i didn't clone the items :p)
    // Delegate to return a listviewitem collection based on the listview passed to it
    private delegate ListView.ListViewItemCollection GetItems(ListView lstview);
    
    // Method called in order to correctly return the listview item collection
            private ListView.ListViewItemCollection getListViewItems(ListView lstview)
            {
                ListView.ListViewItemCollection temp = new ListView.ListViewItemCollection(new ListView());
                if(!lstview.InvokeRequired)
                {
                    foreach(ListViewItem item in lstview.Items)
                    {
                        // Clone each item and add it to my temp array.
                        // If i don't clone it, then i won't be able to use it in my second thread
                        temp.Add((ListViewItem)item.Clone());
                    }
                    return temp;
                }
                else
                {
                    // If i need an invoke, i create a delegate variable, call it
                    // and cast its return to ListViewItemCollection and return that. Problem solved.
                    GetItems d = new GetItems(getListViewItems);
                    return (ListView.ListViewItemCollection)this.Invoke(d, new object[] { lstview });
                }
            }
    


  • Advertisement
Advertisement