none
Exception has been thrown by the target of an invocation. RRS feed

  • 问题

  •     void Back_DoWork(object sender, DoWorkEventArgs e) {
                //The increase value of the progress bar depends on the count of the files.
                //prb.Maximum = ls_paths.Items.Count;
                for (int i = 0; i < prb.Maximum; i++) {
                    System.Threading.Thread.Sleep(100);
                    _worker.ReportProgress(i);
    
                    if (_worker.CancellationPending) {
                        e.Cancel = true;
                        _worker.ReportProgress(0);
                        return;
                    }
                }
    
                MessageBox.Show("Done");
            }
    
    void Back_ProgressChanged(object sender, ProgressChangedEventArgs e) {
                prb.Value = e.ProgressPercentage;
            }
    
     void Back_Completed(object sender, RunWorkerCompletedEventArgs e) {
                btn_move.Enabled = true;
                _worker.ReportProgress(100);
            }
        private void btn_move_Click(object sender, EventArgs e) {
                _worker = new BackgroundWorker();
                _worker.WorkerReportsProgress = true;
                _worker.WorkerSupportsCancellation = true;
                _worker.DoWork += (s, ea) => this.Back_DoWork(s, ea);
                _worker.ProgressChanged += (s, ea) => this.Back_ProgressChanged(s, ea);
                _worker.RunWorkerCompleted += (s, ea) => this.Back_Completed(s, ea);
                _worker.RunWorkerAsync();
                btn_move.Enabled = false;
    
                StringBuilder sbPath = new StringBuilder(); //Path builder.
                for (int i = 0; i < ls_paths.Items.Count; i++) {
                    System.IO.DirectoryInfo dirRoot = new DirectoryInfo((string)ls_paths.Items[i]);
                    if (!dirRoot.Exists) {
                        MessageBox.Show("The directory is not exist!");
                        return;
                    }
                    foreach (FileInfo file in dirRoot.GetFiles()) {
                        //Joint the directory path.
                        sbPath.Append(file.DirectoryName).Append("\\").Append(Path.GetFileNameWithoutExtension(file.Name));
                        //Iterate create new directories.
                        System.IO.DirectoryInfo dir = Directory.CreateDirectory(sbPath.ToString());
                        sbPath.Clear();
                        //Joint the file path.
                        sbPath.Append(dir.FullName).Append("\\").Append(file.Name);
                        file.MoveTo(sbPath.ToString());
                        sbPath.Clear();
                    }
                }
            }
    


    I am trying to remove the specified files of the folders but why it always throw exception(in Application.Run(new FormMain()); of the Main) after complete.
    2014年1月10日 11:39

答案

全部回复

  • Is it "Cross-thread operation not valid" exception? if yes, please try to invoke the code in UI thread when use "prb.Maximum".
    2014年1月12日 16:17
  • Please post the Exception Message and the Stack Trace.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月13日 6:40
    版主