OK before everyone post duplicate let me inform you I have looked at all those other post and im still lost some say use delegates or background worker etc... but how would I make this thread safe i want to delete the files on its own thread.
here is the code that i am working with.
private void button1_Click(object sender, EventArgs e)
{
cleanFiles.RunWorkerAsync();
}
private void cleanFiles_DoWork(object sender, DoWorkEventArgs e)
{
if (listView1.CheckedItems.Count != 0)
{
// If so, loop through all checked files and delete.
for (int x = 0; x <= listView1.CheckedItems.Count - 1; x++)
{
string tempDirectory = Path.GetTempPath();
foreach (ListViewItem item in listView1.CheckedItems)
{
string fileName = item.Text;
string filePath = Path.Combine(tempDirectory, fileName);
try
{
File.Delete(filePath);
}
catch (Exception)
{
//ignore files being in use
}
}
}
PaintListView(tFile);
MessageBox.Show("Files removed");
toolStripStatusLabel1.Text = ("Ready");
}
else
{
MessageBox.Show("Please put a check by the files you want to delete");
}
}