0

I've done a GUI in VisualStudio and used a TextBox to show to user what's happening.

I use myTextBox.AppendText to show information like

myTextBox.AppendText("\n" + DateTime.Now.ToLocalTime() + ": " + serviceName + " waiting for stopping");
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
service.Close();
myTextBox.AppendText("\n" + DateTime.Now.ToLocalTime() + ": " + serviceName + " has been stopped correcly");

and so on. The TextBox, anyway, is filled with the Text only when all jobs are completed. So, when all my code has finished to be run, the TextBox is filled with all the strings. So, I would like to print the string at the time I call AppendText. Are I missing anything? Maybe is anything thread-freezing like in java?

Thank you in adavnce.

user2896152
  • 762
  • 1
  • 9
  • 32
  • 4
    not an asnwer but => using WPF, look at MVVM pattern, do not touch textbox control, bind it to property of ViewModel and work only with that property... – Divisadero May 10 '16 at 12:18
  • "Maybe is anything thread-freezing like in java" - this is because you're doing your job directly on UI thread. You need to run all this code from background thread (use tasks, if you're on the .NET 4+, `BackgroundWorker` otherwise). Also, code will depend on following MVVM or not. IMO, there can't be *good* answer to this question, as it stated. – Dennis May 10 '16 at 12:53

3 Answers3

1

Your problem is that your service calls are (apparently) running on the UI thread, so nothing will show until they have stopped blocking the thread.

You need to put your service calls on a background thread, then change your textbox text by marshalling the change up to the UI thread via the dispatcher

Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
  • Ok thank you. And, how can I do that? Could you tell me what method or pattern I should use? Because in Java I used to use SwingUtilities, method InvokeLater to do that, but in c# I have not found something similar – user2896152 May 10 '16 at 13:11
0

So, when all my code has finished to be run, the TextBox is filled with all the strings. So, I would like to print the string at the time I call AppendText

If I understood that statement correctly, you want to replace the text in the TextBox each time instead of adding some text into it. If that's the case, you should use the TextBox.Text property instead of AppendText

TextBox.Text

Setting this property replaces the contents of the text box with the specified string.

AppendText

use this method to add text to the existing text

Jan Paolo Go
  • 5,842
  • 4
  • 22
  • 50
0

this is what i use

public static class ExtensionMethods
{
   private static Action EmptyDelegate = delegate() { };

   public static void Refresh(this UIElement uiElement)
   {
      uiElement.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
   }
}
private void LoopingMethod()
{
   for (int i = 0; i < 10; i++)
   {
      label1.Content = i.ToString();
      label1.Refresh();
      Thread.Sleep(500);
   }
}

typically the UI does not refresh until the code behind completes
you need to force a refresh

paparazzo
  • 44,497
  • 23
  • 105
  • 176