The problem is that your long-running method is blocking the UI thread. I would suggest that the solution is to use the Task-based Asynchronous Pattern.You need to run your long-running method on its own thread and get it to report its progress to the UI thread in the form of a string
. You can then stick the returned string into a TextBlock
. Here's a demo console project to show you the basic idea.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace DemoAsync
{
class Program
{
private static async Task Main()
{
var progress = new Progress<string>();
progress.ProgressChanged +=
((sender, text) => Console.WriteLine(text));
await Task.Run(() => MyLongRunningMethod(progress));
Console.WriteLine("Hit 'return' to finish");
Console.ReadLine();
}
private static void MyLongRunningMethod(IProgress<string> progress)
{
int max = 10;
for (int i = 1; i <= max; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(1));
progress.Report(i.ToString());
}
}
}
}