简体   繁体   中英

C# - Cross-thread operation not valid. Control ListView…and backgroundworker

I have a backgroundworker copying files from a ListView. I read many similar questions, but they all seem to be about the DoWork property trying to change a UI control. I'm not changing anything. I simply go through the file list in the ListView:

    void BackgroundWorkerDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        this.timerReminder.Stop();
        BackgroundWorker worker = sender as BackgroundWorker;
        worker.ReportProgress(0);

        string msg = "You are about to copy selected files into selected folder.\n\n" + 
            "Press Yes to overwrite existing files if they exist in selected folder or\n" + 
            "Press No to keep files if they exist in selected folder or\n" + 
            "Press Cancel to cancel back up";

        DialogResult dr = MessageBox.Show(msg,
            "Start back up",
            MessageBoxButtons.YesNoCancel,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button1);

        string dt = string.Format("{0:dd-MM-yyyy-hh-mm-ss}", DateTime.Now);
        byte[] buffer = new byte[64 * 1024];

        ListView.CheckedListViewItemCollection checkedItems = this.listViewFiles.CheckedItems;
        //int count = checkedItems.Count;

        switch (dr)
        {
            case DialogResult.Yes:
                foreach (ListViewItem item in checkedItems)
                {
                    string file = item.SubItems[0].Text;
                    if (File.Exists(file) && worker.CancellationPending == false)
                    {
                        using (FileStream source = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            long fileLength = source.Length;
                            string fn = Path.Combine(this.savePath, Path.GetFileName(file));
                            using (FileStream destination = new FileStream(fn, FileMode.OpenOrCreate, FileAccess.Write))
                            {
                                long totalBytes = 0;
                                int currentBlockSize = 0;

                                while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    totalBytes += currentBlockSize;     
                                    destination.Write(buffer, 0, currentBlockSize);
                                    worker.ReportProgress((int)(100.0 * (double)totalBytes / fileLength));
                                }
                            }
                        }
                    }
                    else{
                        e.Cancel = true;
                        break;
                    }
                }
                break;

            case DialogResult.No:
                foreach (ListViewItem item in checkedItems)
                {
                    string file = item.SubItems[0].Text;
                    if (File.Exists(file) && worker.CancellationPending == false)
                    {
                        using (FileStream source = new FileStream(file, FileMode.Open, FileAccess.Read))
                        {
                            long fileLength = source.Length;
                            string fn = Path.Combine(this.savePath, Path.GetFileNameWithoutExtension(file) + "-" + dt + Path.GetExtension(file));
                            using (FileStream destination = new FileStream(fn, FileMode.Create, FileAccess.Write))
                            {
                                long totalBytes = 0;
                                int currentBlockSize = 0;

                                while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    totalBytes += currentBlockSize;     
                                    destination.Write(buffer, 0, currentBlockSize);
                                    worker.ReportProgress((int)(100.0 * (double)totalBytes / fileLength));
                                }
                            }
                        }
                    }
                    else{
                        e.Cancel = true;
                        break;
                    }
                }
                break;

            case DialogResult.Cancel:
                e.Cancel = true;
                this.backgroundWorker.CancelAsync();
                break;
        }
    }

What is wrong with that? Do I need the invoke to read controls too? If yes, will really appreciate your help how to do that. Thank you.

[EDIT] I also tried this:

        private BlockingCollection<ListView.CheckedListViewItemCollection> checkedItems = null;
        ...
        this.checkedItems = new BlockingCollection<ListView.CheckedListViewItemCollection>();
        foreach (ListView.CheckedListViewItemCollection item in this.listViewFiles.CheckedItems)
        {
            this.checkedItems.Add(item);
        }
        this.checkedItems.CompleteAdding();

but I have the following error at the foreach: "System.InvalidCastException: Unable to cast object of type 'System.Windows.Forms.ListViewItem' to type 'CheckedListViewItemCollection'."

ListView.CheckedListViewItemCollection is not a thread-safe collection. You're going to want to put the contents in something from the System.Collections.Concurrent namespace, like ConcurrentBag<T> or BlockingCollection<T> .

MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM