0

I need to access a listview's items in a DoWork event handler. For this the delegate and the method to invoke the listview what I wrote is:

delegate ListView itemDelegate(ListView bufferedListView1);

   private ListView getItems(ListView bufferedListView1)
   {
       if (bufferedListView1.InvokeRequired)
       {
         //  BeginInvoke(new itemDelegate(getItems));
             bufferedListView1.Invoke(new itemDelegate(getItems));
       }
       else
       {
           return bufferedListView1;
       }
   }

This is the first time I am working with invoking a control. So please let me know where I am wrong. One error that I get is gsm_modem.Form1.getItems(System.Windows.Forms.ListView): not all code paths return a value. I even guess that what I wrote might be wrong. Correction please..

Shiridish
  • 4,942
  • 5
  • 33
  • 64
  • The IF part of your statement doesn't return any value but the method expects to return a ListView. You'll need to either set a void return type on your method or return a listview. – Jamie Dixon Aug 18 '12 at 08:46
  • @JamieDixon I do need to return the listview because I need to use it in another thread. So what should I write in BeginInvoke()? – Shiridish Aug 18 '12 at 08:48
  • You can't use BeginInvoke, Invoke is required here so you can get its return value. Good hint what is so wrong about doing this kind of code, get UI values *before* you start a worker. Getting them *while* the user is using the UI just produces random infrequent failure. – Hans Passant Aug 18 '12 at 08:53
  • http://stackoverflow.com/questions/661561/how-to-update-gui-from-another-thread-in-c – Ravi Patel Aug 18 '12 at 08:54
  • @HansPassant I have changed it to Contol.Invoke and I still get the same error. This might be an easy task `get UI values before you start a worker`, but I really cant understand how to do it. Please elaborate – Shiridish Aug 18 '12 at 09:09
  • @RaviPatel : sorry but that dint help me how to read a listview in other thread. A simpler code would be of much help – Shiridish Aug 18 '12 at 09:14
  • are you want text value of listviewitem? – Ravi Patel Aug 18 '12 at 09:17
  • @RaviPatel I want to acess the **complete listview** (In a `foreach loop`) in the `DoWork` handler of `Background worker`. – Shiridish Aug 18 '12 at 09:21
  • why you need whole listview if you want don't want to update it? – Ravi Patel Aug 18 '12 at 09:25
  • @RaviPatel I got nothing to update. I only need to access the listview in other thread. So please let me know how to go about – Shiridish Aug 18 '12 at 09:27
  • _"I want to acess the complete listview [in] Background worker"_ - that's jus not going to work. ListView is a Control means main-thread-only. Try to separate Nodel and GUI. – H H Aug 19 '12 at 08:14

2 Answers2

2

You can do something like this

First create shared variable in global scope for your form.

List<string> listItems;

Now before calling RunWorkerAsync do following

listItems = new List<string>();
foreach (ListViewItem item in bufferedListView1.Items)
            {
                //If you want to add tag to list then you can use dictionary like Dictionary<string, object) listItems; and then add items as listItems.Add(item.Text, item.Tag); It only works if text is unique.
                listItems.Add(item.Text);
            }
bgw1.RunWorkerAsync();

Now read the list inside background worker using foreach.

Ravi Patel
  • 2,136
  • 3
  • 32
  • 48
0

Thanks to @Ravi patel for the idea. This is what I did to solve the problem:

ListView listItems = new ListView();\\In global scope

foreach (ListViewItem item in bufferedListView1.Items)
{
   listItems.Items.Add((ListViewItem)item.Clone()); // Copied the bufferedListview's items that are to be accessed in other thread to another listview- listItems
}

Then used listItems in my other thread easily.

Shiridish
  • 4,942
  • 5
  • 33
  • 64