0

The usual practice is to show the progress window (with a progress bar) in the UI thread and update the progress from the worker thread.

I have a lot of long operations which are started and run in the GUI thread itself(which temporarily freeze the GUI). The requirement is to show the progress bar for all existing long operations. The usual solution would be to move the long operations as threads and update the progress from there. But i am not sure about the thread safety of those long operations.

Is there a way where we show the progress window in another thread (so it doesn't freeze) and then update the progress from the main GUI thread itself?

Socrates
  • 359
  • 6
  • 17
  • I'd suggest that it would be better in the long run for you to refactor your long-running code out of the UI and into separate classes that can be run on a different thread. It'll save you a lot of grief and help you to write better code in the future. – Enigmativity Sep 12 '12 at 07:07
  • I would also wish to do so. But these operations were written by somebody else in the past involving files, database, UI controls and every susceptible non thread safe things. Time and effort doesn't favor disturbing the status-quo. – Socrates Sep 12 '12 at 07:20
  • I would be inclined to differ, but I'm sure you would have a better idea of the complexity than I. I would think though, that you're throwing good developer time after bad and just making the situation worse. Learning to refactor complicated systems is a skill that you get better at with practice. – Enigmativity Sep 12 '12 at 08:58

2 Answers2

1

I dont know a solution to show the ProgressBar in another thread, but a hack you can try ist to let the system execute its actions (Update the UI) from within your long running operations. For this, you can call the following function repeatedly from within your long running operations:

public static void DoEvents() {
  DispatcherFrame frame = new DispatcherFrame();
  Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) {
    frame.Continue = false;
    return null;
}), null);
  Dispatcher.PushFrame(frame);
}

But take care, this is not a nice way to resolve the problem. Better to chose an appropriate design.

HCL
  • 36,053
  • 27
  • 163
  • 213
1

Check this thread

I think you should use the Application.DoEvents()

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44
  • This is winforms. The OP has tagged it's question as WPF and WPF has no Application.DoEvents. That's why I posted a replacement function. But as mentioned: It's only a workaround. – HCL Sep 12 '12 at 07:28
  • Use of `Application.DoEvents()` is extremely bad practice and can cause all sorts of issues with things like re-entrancy. – Enigmativity Sep 12 '12 at 09:00
  • mmhmm so what should we do, if we want to move the progress bar in the UI with the thread? by the way, i apologize for my answer, i thought this is a winform :) – spajce Sep 12 '12 at 09:34