Having trouble asynchronously updating a wpf text block.

So I'm trying to update a textblock and struggling a bit. This is using WPF not windows forms. I can get the text block to update when a method returns, but cannot update it while a method actively running. From what I have found so far it looks like I need to use delegates, but so far all my attempts to do so have failed.

What I have right now is for the delegate function

 public void UpdateBuildOutputText(string updateText) { buildOutput.Text += updateText; } public delegate void delUpdateBuildOutputText(string message); 

and

 private void TestButton(object sender, RoutedEventArgs e) { delUpdateBuildOutputText outputText = UpdateBuildOutputText; for (int i = 0; i < 10; i++) { outputText("step " + i + "\n"); System.Threading.Thread.Sleep(1000); } } 

as a test case. The idea being that it outputs a line then waits a second then outputs another line and soforth instead of waiting 10 seconds then writing 10 lines.

Obviously my actual use case has a lot more going on, but if I can't get it to work in a test case then no point in putting it into more complicated code.

Any help would be appreciated, I'm still pretty new to C#, coming from a VHDL background which doesn't have threads or guis or anything.

Thanks

by internet_observer via /r/csharp

Leave a Reply