首页 > 解决方案 > C# - 从线程更新 windows 窗体元素

问题描述

我目前正在创建一个程序来将笔记本电脑备份到 U 盘。我创建了一个从中调用方法的类。然后我通过一个单独的线程开始实际的备份。目前,我正在尝试通过此线程更改文本框和进度条。但是,它总是只立即显示第一个更改,而只有在程序运行时才显示其他进度。我从互联网上尝试了几种解决方案,但到目前为止没有任何效果。也许有人在这里有解决方案。

    backup sales_backup = new backup();

    //Start Backup Button
    private void backup_button_Click(object sender, EventArgs e)
    {
        Thread backupprocess = new Thread(new 
        ThreadStart(sales_backup.backup_start));
        backupprocess.Start();
    }

    //Backup Function
        public void backup_start()
        {
            files_to_copy = 0;
            files_copied = 0;

            var principalForm = System.Windows.Forms.Application.OpenForms.OfType<Form1>().FirstOrDefault();

            if (principalForm.drive_selector.SelectedItem != null)
            {
                //Set Parameters
                principalForm.backup_button.Visible = false;
                error_copy = false;
                error_message = "";
                device_removed = false;

                //Fill variables
                string temp = principalForm.drive_selector.SelectedItem.ToString();
                temp = regex_matching_return_match(temp, @"[A-Z,a-z](:\\)");
                backup_device = temp;

                //Set Backup device size
                for (int i = 0; i < backup_devices_list.Count; i++)
                {
                    if (backup_devices_list[i].backup_device_name == temp)
                    {
                        backup_device_size = backup_devices_list[i].device_size;
                        file_system = backup_devices_list[i].file_system;
                        double temp_free = calculate_GB(backup_devices_list[i].device_free_space.ToString());
                        device_free_space = temp_free;
                        break;
                    }
                }

                //If no device is initialized
                if (backup_device == null || backup_device_size == 0)
                {
                    write_to_textbox(get_create_usb_instance_error(), "red");
                }
                else //If select ist successfull
                {
                    //Get Backup size
                    get_size();
                    if (backup_size < device_free_space)
                    {
                        backup_path_target = backup_device + "\\Backup\\";
                        Directory.CreateDirectory(backup_path_target);

                        //Get file count
                        get_file_count();

                        //Create Copy job
                        for (int i = 0; i < backup_path_source.Length; i++)
                        {
                            string backup_path_s = backup_path_source[i] + "\\";
                            string backup_path_t = backup_path_target + backup_path_target_folders[i] + "\\";
                            copy_function(backup_path_s, backup_path_t);
                            int progress = return_progress();
                            TextBox test = principalForm.textBox2;
                            ProgressBar progress_bar = principalForm.progressBar1;
                            //Delegate Textbox
                            if (test.InvokeRequired)
                            {
                                test.Invoke(new Action(() => test.Text = "Copying: " + backup_path_t));
                            }
                            else
                            {
                                test.Text = "Copying: " + backup_path_t;
                            }
                            //Delegate Progressbar
                            if (progress_bar.InvokeRequired)
                            {
                                test.Invoke(new Action(() => progress_bar.Value = progress));
                            }
                            else
                            {
                                progress_bar.Value = progress;
                            }
                        }

标签: c#windowsmultithreadingforms

解决方案


我无法对此进行测试,但它至少应该可以帮助您到达那里:

private void InvokeIfRequired<C>(C control, Action<C> action) where C : Control
{
    if (control.InvokeRequired)
    {
        control.Invoke((Action)(() => action(control)));
    }
    else
    {
        action(control);
    }
}

private void backup_button_Click(object sender, EventArgs e)
{
    var principalForm = System.Windows.Forms.Application.OpenForms.OfType<Form1>().FirstOrDefault();
    if (principalForm.drive_selector.SelectedItem != null)
    {
        principalForm.backup_button.Visible = false;

        string temp = principalForm.drive_selector.SelectedItem.ToString();

        TextBox test = principalForm.textBox2;
        ProgressBar progress_bar = principalForm.progressBar1;

        Action<string> updateTest = t => this.InvokeIfRequired<TextBox>(test, c => c.Text = t);
        Action<int> updateProgress = v => this.InvokeIfRequired<ProgressBar>(progress_bar, c => c.Value = v);

        Thread backupprocess = new Thread(new ThreadStart(() => sales_backup.backup_start(temp, updateTest, updateProgress)));
        backupprocess.Start();
    }
}

//Backup Function
public void backup_start(string temp, Action<string> updateTest, Action<int> updateProgress)
{
    files_to_copy = 0;
    files_copied = 0;

    //Set Parameters

    error_copy = false;
    error_message = "";
    device_removed = false;

    //Fill variables
    temp = regex_matching_return_match(temp, @"[A-Z,a-z](:\\)");
    backup_device = temp;

    //Set Backup device size
    for (int i = 0; i < backup_devices_list.Count; i++)
    {
        if (backup_devices_list[i].backup_device_name == temp)
        {
            backup_device_size = backup_devices_list[i].device_size;
            file_system = backup_devices_list[i].file_system;
            double temp_free = calculate_GB(backup_devices_list[i].device_free_space.ToString());
            device_free_space = temp_free;
            break;
        }
    }

    //If no device is initialized
    if (backup_device == null || backup_device_size == 0)
    {
        write_to_textbox(get_create_usb_instance_error(), "red");
    }
    else //If select ist successfull
    {
        //Get Backup size
        get_size();
        if (backup_size < device_free_space)
        {
            backup_path_target = backup_device + "\\Backup\\";
            Directory.CreateDirectory(backup_path_target);

            //Get file count
            get_file_count();

            //Create Copy job
            for (int i = 0; i < backup_path_source.Length; i++)
            {
                string backup_path_s = backup_path_source[i] + "\\";
                string backup_path_t = backup_path_target + backup_path_target_folders[i] + "\\";
                copy_function(backup_path_s, backup_path_t);
                int progress = return_progress();

                //Delegate Textbox
                updateTest("Copying: " + backup_path_t);

                //Delegate Progressbar
                updateProgress(progress);
            }
        }
    }
}

推荐阅读