2 Replies - 952 Views - Last Post: 28 April 2013 - 03:21 AM Rate Topic: -----

#1 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Exception: Cross-thread operation not valid

Posted 28 April 2013 - 02:54 AM

Well I'm working on an application which grabs a bunch of file names from a website and then takes a file specified by the user and makes a copy of it for each of the names it grabs. These names are added to a ListView and I need to access them from a separate thread than the one the ListView is on to prevent my application from hanging.

Here is the code I currently have for this task.
private void renameTarget()
  {
  var target = (string)targetBox.Invoke(new Func<string>(() => targetBox.Text)); 
  var destination = (string)destinationBox.Invoke(new Func<string>(() => destinationBox.Text)); 
  var list = (ListView.ListViewItemCollection)nameView.Invoke(new Func<ListView.ListViewItemCollection>(() => nameView.Items));
  foreach(ListViewItem itm in list)
   {
   File.Copy(target, destination + itm.Text + Path.GetExtension(target));
   }
  }



The exception is raised in the foreach statement. I'm pretty sure that my invoke is just incorrect for what I'm trying to do with my ListView, but it's almost 5:00 AM and I'm just tired of thinking. xD

I was hoping I could get a bit of assistance from the awesome members of this forum. :)/>

Thank you for taking the time to look into my problem. n_n

Is This A Good Question/Topic? 0
  • +

Replies To: Exception: Cross-thread operation not valid

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Exception: Cross-thread operation not valid

Posted 28 April 2013 - 03:11 AM

The problem is caused by trying to access a UI item (in this case, the ListView named "list") from a different thread.

The UI thread is always separate from any other threads, to keep a responsive interface. You'll need to call Invoke on the UI element, or (even better) get the elements from the ListView into a different variable before starting the worker thread.

EDIT: Sorry, I see that you're attempting to call invoke, but as a member of the ListView "nameView". That won't work - you need to call invoke on the UI thread to access one of its members. Try a MethodInvoker delegate.

This post has been edited by MrShoes: 28 April 2013 - 03:13 AM

Was This Post Helpful? 2
  • +
  • -

#3 Opacity  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-April 12

Re: Exception: Cross-thread operation not valid

Posted 28 April 2013 - 03:21 AM

Nevermind. I figured it out. xD

I wrote up the following bit of code
private delegate ListView.ListViewItemCollection GetItems(ListView lv);

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



Then called it as follows
foreach(ListViewItem item in getListViewItems(List))
 {
 //...
 }



By the way, I am fully aware of my unnecessary code blocks and such. When I started programming I started with nothing more than a compiler, some books, and notepad. (I didn't start with C#, I've only been using that for a few years.) That's just the way I formatted everything and I've become used to it. I don't really see it as a huge problem though, unless of course I'm working with a team or something, but I don't have to worry about that right now. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1