Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got a loop wherein I store some files in a compressed file and it sometimes takes a while. I attempted to place a notification of what file was being handled in a text box so the user knows that progress is being made. But the text refuses to present itself.

The loop is in the main thread so I shouldn't need a callback function.

I've been away from programming for three years but I don't recall having this problem back then.

What I have tried:

Created a callback function anyway but Invoke is never required.
Posted
Updated 12-Sep-19 21:39pm

The correct way to do this would be to move the loop to a background Task or use a BackgroundWorker[^].

What you're doing is keeping the "long running" work on the UI thread, preventing the UI controls from responding to the messages that tell them to repaint themselves with new data.
 
Share this answer
 
You can use Application.DoEvents() in Windows Forms, see example here: Application.DoEvents Method (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 

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.

C#"
using System;
using System.Threading;
using System.Threading.Tasks;

namespace DemoAsync
{
    // To get async Main to run you may need to change the language version
    //In Solution Explorer/Properties/Build/Advanced 
    //Select 'C# latest supported minor version (latest)' in the Language Version list
    class Program
    {
        private static async Task Main()
        {
            var progress = new Progress<string>();
            //Define the handler. It will be run on the UI  thread
            //In this example the handler is writing the text to the console
            //But you can put it in a TextBox
            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;
            //simulation of a long-running method
            //It reports progress every second
            for (int i = 1; i <= max; i++)
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));

                progress.Report(i.ToString());
            }


        }
    }
}

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900