none
Forcing immediate display update of a form RRS feed

  • Question

  • I want to display some pictures while a long process executes in a separate worker thread. I use the statement dlg.Update() after I change the display in the form dlg at certain intervals of time. The trouble is that this statement is executed asynchronously, so the display is not updated until the worker thread ends. Is there a way to force an immediate update?
    Saturday, November 17, 2012 8:52 PM

Answers

  • Thanks for your efforts to help me. Here is a bit of my code. Hopefully you can see my attempts at getting this to work properly. All I can see is the first picture, at the end of the worker thread I can see the rest of the pictures in a fast succession. I hope you can tell me where I went wrong.

    ThreadStart entryPoint = new ThreadStart(runLongProcess);

    Thread longProcessThread = new Thread(entryPoint);

    portfolioForm dlg = new portfolioForm();

    while (true){

       Thread.Sleep(500);

       if (processComplete)

          break;

       if (count / 100 * count == count) {

          dlg.Hide();

          FileInfo file = files[fileCount++];

          dlg.displayPicture(file.FullName);

          dlg.Show();

          dlg.Update();

          // dlg.Refresh();

       }

       count++;

    longProcessThread.Start();

    Yes, the  problem is in your code.  It might be in one of your other fragments, but this code won't compile without them.
    Monday, November 19, 2012 9:44 PM
  • "The trouble is that this statement is executed asynchronously, so the display is not updated until the worker thread ends.

    Something's wrong.  If you are calling  Update() from the UI thread, OnPaint should be called immediately.  Calling Update from another thread is illegal.

    Update should cause an immediate call of OnPaint.  Use Invalidate() with Update to repaint a portion of the Form.  Use Refresh to invalidate the entire Form and update. 

    Saturday, November 17, 2012 9:53 PM

All replies

  • Call the Invalidate() method on the controls that you wish to have re-Paint themselves. Make sure that all your display code is in appropriate Paint event handlers.

    "Premature optimization is the root of all evil." - Knuth

    If I provoked thought, please click the green arrow

    If I provoked Aha! please click Propose as Answer

    Saturday, November 17, 2012 9:39 PM
  • "The trouble is that this statement is executed asynchronously, so the display is not updated until the worker thread ends.

    Something's wrong.  If you are calling  Update() from the UI thread, OnPaint should be called immediately.  Calling Update from another thread is illegal.

    Update should cause an immediate call of OnPaint.  Use Invalidate() with Update to repaint a portion of the Form.  Use Refresh to invalidate the entire Form and update. 

    Saturday, November 17, 2012 9:53 PM
  • Thanks for your efforts to help me. Here is a bit of my code. Hopefully you can see my attempts at getting this to work properly. All I can see is the first picture, at the end of the worker thread I can see the rest of the pictures in a fast succession. I hope you can tell me where I went wrong.

    ThreadStart entryPoint = new ThreadStart(runLongProcess);

    Thread longProcessThread = new Thread(entryPoint);

    longProcessThread.Start();

    portfolioForm dlg = new portfolioForm();

    while (true){

       Thread.Sleep(500);

       if (processComplete)

          break;

       if (count / 100 * count == count) {

          dlg.Hide();

          FileInfo file = files[fileCount++];

          dlg.displayPicture(file.FullName);

          dlg.Show();

          dlg.Update();

          // dlg.Refresh();

       }

       count++;

    Monday, November 19, 2012 9:22 PM
  • Thanks for your efforts to help me. Here is a bit of my code. Hopefully you can see my attempts at getting this to work properly. All I can see is the first picture, at the end of the worker thread I can see the rest of the pictures in a fast succession. I hope you can tell me where I went wrong.

    ThreadStart entryPoint = new ThreadStart(runLongProcess);

    Thread longProcessThread = new Thread(entryPoint);

    portfolioForm dlg = new portfolioForm();

    while (true){

       Thread.Sleep(500);

       if (processComplete)

          break;

       if (count / 100 * count == count) {

          dlg.Hide();

          FileInfo file = files[fileCount++];

          dlg.displayPicture(file.FullName);

          dlg.Show();

          dlg.Update();

          // dlg.Refresh();

       }

       count++;

    longProcessThread.Start();

    Yes, the  problem is in your code.  It might be in one of your other fragments, but this code won't compile without them.
    Monday, November 19, 2012 9:44 PM