Foreach 行在 backgroundworker C# WPF 中的文本框中

Foreach Line in textbox inside backgroundworker C# WPF

我有多行文本框,我可以使用它获取每行的值(以及索引值);

public void doProcess()
{ 
        if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
        {

            string _deliverynumbers = deliverynumbers.Text;
            string[] delimiter = {Environment.NewLine};
            string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);

            int count = array_deliverynumbers.Length;
            int current = 0;

            foreach (string text in array_deliverynumbers)
            {
                    if (!string.IsNullOrEmpty(Convert.ToString(text)))
                    {

                        current++;
                        Double Total_Percentage = count;
                        Double Current_Percentage = current;
                        Double progressAmount = (Current_Percentage / Total_Percentage * 100);
                        //backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));

                        //could use this to update progress bar
                        //progress.Value = progressAmount;

                        //Do some other processes like for each line, create a file and name it to the value of the line value...


                    }

                }


        }

 }

然而,当我在 DoWork 调用中尝试 doProgress() 时,我得到“'System.InvalidOperationException' 类型的第一次机会异常...下面是 DoWork 代码;

private void DoWork(object sender, DoWorkEventArgs e)
    {
        //This works
        for (int i = 0; i <= 100; i++)
        {
            Thread.Sleep(100);
            backgroundWorker.ReportProgress(i);
        }

        //But this causes the exception
        doProgress();
    }

我什至删除了 doProgress() 代码的各个部分以查看哪些有效但 none 它确实...

编辑

即使(!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text))) {} 打破它..

private void DoWork(object sender, DoWorkEventArgs e)
    {
        if (!string.IsNullOrEmpty(Convert.ToString(deliverynumbers.Text)))
        {

        }
    }

成功了...如下所示...

基本上这从 2 个文本框(一个是路径值,一个是文件路径值)和一个多行文本框获取值,对于多行文本框中的每一行,将文件(文件路径文本框值)复制到文件夹路径(路径文本框值)并将复制的文件重命名为该行的值(来自多行文本框)。

简而言之,从多行文本框中的列表中批量复制文件并将其重命名到所选文件夹

我的点击事件

public void begin_process_Click(object sender, RoutedEventArgs e)
    {

        //Hide Form
        form_process.Visibility = Visibility.Hidden;

        //Show Loader (with progress bar)
        loader.Visibility = Visibility.Visible;

        //Get Multiline Text Box Value
        string _deliverynumbers = deliverynumbers.Text;

        //Get source file path
        string sourceFile = file_path.Text;

        //Get copy to path
        string destinationPath = share_drive_folder_path.Text;

        //Create object to pass through backgroundWorker.RunWorkerAsync(args);
        Object[] args = { _deliverynumbers, sourceFile, destinationPath };

        //pass "args" through..
        backgroundWorker.RunWorkerAsync(args);

    }

在我的 DoWork() 方法中 - 仍然需要异常捕获和额外的文件夹和文件检查等

private void DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        //Get arguments we passed in onclick event
        Object[] arg = e.Argument as Object[];

        //Do stuff for multiline value - which is via (string)arg[0]
        string[] delimiter = { Environment.NewLine };
        string _deliverynumbers = (string)arg[0];
        string[] array_deliverynumbers = _deliverynumbers.Split(delimiter, StringSplitOptions.None);

        //Generate Indexing
        int count = array_deliverynumbers.Length;
        int current = 0;

        //ForEach Line in multiline TextBox
        foreach (string text in array_deliverynumbers)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(text)))
            {

                //Update Indexing
                current++;

                //Determine Progress bar percentage
                Double Total_Percentage = count;
                Double Current_Percentage = current;
                Double progressAmount = (Current_Percentage / Total_Percentage * 100);

                //Copy source file for each line in TextBox & rename new copied file to the line value & source file format
                string new_filename = text.Substring(2, text.Length - 2);
                string sourceFile = (string)arg[1];
                string destinationPath = (string)arg[2];
                string destinationFileName = new_filename + System.IO.Path.GetExtension(sourceFile);
                string destinationFile = System.IO.Path.Combine(destinationPath, destinationFileName);

                //Check Folder exists
                if (!System.IO.Directory.Exists(destinationPath))
                {
                    System.IO.Directory.CreateDirectory(destinationPath);
                }

                //Copy & Move and rename
                System.IO.File.Copy(sourceFile, destinationFile, true);

                //Sleep for a bit incase processing is to quick
                Thread.Sleep(100);


                //Update progress bar with new amount                    
                backgroundWorker.ReportProgress(Convert.ToInt32(progressAmount));


            }

        }


    }