-3

While I'm launching a process I can't controls my textboxes.

My question is: How can I modify a textbox while it's launching a process?

Even when I try to modify the textbox before the process is launched it still doesn't work.

I found some answers on this forum but the explanation 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");

    }


}
pb2q
  • 58,613
  • 19
  • 146
  • 147
Bo0m3r
  • 1
  • 2
  • 3
    Look into using a [BackGround Worker](http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx) – Mark Hall Oct 03 '12 at 15:00
  • 1. Tags don't belong in the title. 2. You did not ask a question, nor describe the problem in any way. 3. Read [this](http://stackoverflow.com/questions/11923865/how-to-deal-with-cross-thread-access-exceptions) if you have cross-thread problems. – H.B. Oct 03 '12 at 15:00
  • H.B., I tried your dispatcher test code but again this code doesn't run when it's executing the BAT file. – Bo0m3r Oct 03 '12 at 16:08
  • @Bo0m3r: If it does not run at all the problem is the process, also you should not really use the dispatcher anyway. (also use `@Name`, otherwise there are no notifications) – H.B. Oct 03 '12 at 16:23

1 Answers1

0

When I replace body of your status() method with:

Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Send, new Action(() => { tb_Status.Text = "The backup is running!"; }));
Application.DoEvents();

it works correctly.

rocky
  • 7,506
  • 3
  • 33
  • 48