C# - 跨线程操作无效。 Control ListView ...和backgroundworker

时间:2016-05-03 12:42:11

标签: c# listview backgroundworker

我有一个后台工作者从ListView复制文件。我读了许多类似的问题,但它们似乎都是关于尝试更改UI控件的DoWork属性。我没有改变任何事情。我只需浏览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;
        }
    }

这有什么问题?我是否还需要调用来读取控件?如果是的话,非常感谢您的帮助。谢谢。

[编辑] 我也试过这个:

        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();

但我在foreach有以下错误: &#34; System.InvalidCastException:无法转换类型为&#39; System.Windows.Forms.ListViewItem&#39;的对象键入&#39; CheckedListViewItemCollection&#39;。&#34;

1 个答案:

答案 0 :(得分:0)

ListView.CheckedListViewItemCollection不是线程安全的集合。您希望将内容放在System.Collections.Concurrent命名空间中,例如ConcurrentBag<T>BlockingCollection<T>

MSDN