C# WPF Unable to control Textboxes
- by Bo0m3r
I'm a beginner in coding into C#. While I'm launching a process I can't controls my textboxes.
I found some answers on this forum but the explaination is a bit to difficult for me to implement it for my problem.
I created a small program that will run a batch file to make a backup. While the backup is running I can't modify my textboxes, disabling buttons etc...
I already saw that this is normal but I don't know how to implement the solutions. My last attempt was with Dispatcher.invoke as you can see below.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
tb_Status.Text = "Ready";
}
public void status()
{
Dispatcher.Invoke(DispatcherPriority.Send, new Action( () => { tb_Status.Text = "The backup is running!"; } ) );
}
public void process()
{
try
{
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Robocopy.bat";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
tb_Output.Text = File.ReadAllText("Backup\\log.txt");
}
catch (Exception ex)
{
tb_Status.Text = ex.Message.ToString();
}
}
private void Bt_Start_Click(object sender, EventArgs e)
{
status();
Directory.CreateDirectory("Backup");
process();
tb_Status.Text = "The backup finished";
File.Delete("Backup\\log.txt");
}
}
}
Any help is appreciated!