I am currently creating a program to back up laptops to a USB stick. I have created a class from which I call methods. I then start the actual backup via a separate thread. At the moment I'm trying to change a textbox and a progress bar via this thread. However, it always only shows the first change immediately and the other progress only when the program has run through. I have tried several solutions from the internet but nothing has worked so far. Maybe someone has a solution here.
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;
}
}