Quantcast
Viewing all articles
Browse latest Browse all 18858

ListView not updating white copying files in different thread

The Listview updates at the end if I have copy running in main thread, but it does not update at all if running in different thread:

public void btnPaste_Click(object sender, EventArgs e)
{
  Thread t = new Thread(new ThreadStart(ExecPaste));
  t.Start();  
}

private void ExecPaste()
{
  int i = -1;
  var view = CollectionViewSource.GetDefaultView(CopyList[0]._ListCollection);
  foreach (cListEntry item in CopyList[0]._ListCollection)
  {
    i++;
    String sourceFile = System.IO.Path.Combine(item.FolderFrom, item.FileName);
    String destFile = System.IO.Path.Combine(mPath, item.FileName);
    try
    {
      cListEntry tempitem = CopyList[0]._ListCollection[i];
       tempitem.Status = "In Progress...";
       view = CollectionViewSource.GetDefaultView(CopyList[0]._ListCollection);
       view.Refresh();

      System.IO.File.Copy(sourceFile, destFile, true);

       tempitem.Status = "OK";
       view = CollectionViewSource.GetDefaultView(CopyList[0]._ListCollection);
       view.Refresh();

    }
    catch (System.IO.IOException ex)
    {
      cListEntry tempitem = CopyList[0]._ListCollection[i];
      tempitem.Status = ex.Message.ToString();
    }
  }
}
I expected the listview to update every time I call view.Refresh(); but it is not.

Another problem I created with the thread is that only the first time I click Copy work, the second time I get error:

This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.

at

CopyList[0]._ListCollection.Add(tempitem);

What am I missing here?


Viewing all articles
Browse latest Browse all 18858

Trending Articles