Hi,

I've wrote a small application which browse a folder and rename the files within that foldrer. However if the folder more then 500 files then the app tends to take a while to process.

I somehow or the other needs to show some sort of text displaying "processing..." or show an animated image so that the user can see somethign happening. Otherwise one just feels the application has got stuck and is not responding

Can anyone help me with that. I've tried to put a label and updates its text the the proces starts but it doesn't work out, even tried invalidating the label control.

I've tried doing the same but still nothing happens. The text is displayed when the application completes the full task.

Thx
a

Dani AI

Generated

Long-running rename loops block the WinForms message pump, so labels, animated GIFs and progress bars won't redraw until that loop finishes. 's message-pump suggestion can force a repaint briefly, but it introduces reentrancy and other risks. The robust pattern is to run the work off the UI thread and marshal only small progress updates back to the UI — which is what fixed with "safe thread calls."

A compact BackgroundWorker pattern that reports percent and a simple item count (throttled to avoid thrash):

var bw = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };

bw.DoWork += (s, e) =>
{
    var files = Directory.GetFiles(folderPath);
    int lastPct = -1;
    for (int i = 0; i < files.Length; i++)
    {
        if (bw.CancellationPending) { e.Cancel = true; break; }
        RenameFile(files[i]);
        int pct = (i + 1) * 100 / files.Length;
        if (pct != lastPct) { bw.ReportProgress(pct, i + 1); lastPct = pct; }
    }
};

bw.ProgressChanged += (s, e) =>
{
    progressBar.Value = e.ProgressPercentage;
    statusLabel.Text = string.Format("Processing {0} of {1}", e.UserState, totalFiles);
};

bw.RunWorkerCompleted += (s, e) =>
{
    // re-enable UI, check e.Error / e.Cancelled
};

bw.RunWorkerAsync();

For newer code use Task.Run plus IProgress<T> / Progress<T> to marshal updates back to the UI thread. Practical tips: update the UI only when the percent changes (or every N files) to avoid performance hits; disable controls while processing and re-enable in the completion handler; support cancellation; and surface exceptions cleanly. If you don't know total work, use a marquee ProgressBar or a small animated indicator, but remember animation still requires the UI thread to remain responsive.

See the BackgroundWorker docs and the Progress<T> pattern for details: BackgroundWorker (System.ComponentModel) and Progress<T> / IProgress<T>.

Recommended Answers

All 2 Replies

Place an Applicaiton.DoEvents(); inside of the loop so that the main thread has a chance to service its message queue and this should allow your label to update.

// Jerry

Thanks got that worked out. I had to make safe thread calls.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.