wouterk

wouterk

  • NA
  • 13
  • 30.7k

How to share data with background worker c#

Nov 10 2010 7:45 AM
Hello I´m using a backgroundworker to load tumbnail images on to a listView which is on a form.

but i cant access the listview component from the backgroundworker.

         private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FileSearch searcher = new FileSearch();
searcher.SearchExtensions.AddRange(new string[] { ".jpg", ".bmp", ".png" });
searcher.Recursive = false; // do not include subfolders!

FileInfo[] files = searcher.Search(folderPath);

ImageList ilist = new ImageList();
listView1.LargeImageList = ilist;

//listView appearance settings
ilist.ImageSize = new Size(128, 64);
ilist.ColorDepth = ColorDepth.Depth24Bit;
ListView_SetSpacing(listView1, 128 + 2, 64 + 30);

for (int i = 0; i < files.GetLength(0); i++)
{
Image img = Image.FromFile(files[i].FullName);
ilist.Images.Add(img.GetThumbnailImage(128, 64, null, new System.IntPtr()));
img.Dispose();
ListViewItem lvi = new ListViewItem(files[i].Name);
lvi.ImageIndex = i;
listView1.Items.Add(lvi);
backgroundWorker1.ReportProgress((i*100)/files.Length);
}
}

so listView1 (as all the other components on my form)  can't be accesed from 'private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)'

What would be the correct way to handle this?

Thanks in advance :)

Answers (5)