Solved

How to force a WPF Control to Refresh mid-processing

Posted on 2006-11-24
6
5,923 Views
Last Modified: 2012-06-27
Peace be with you all,

In trying to adapt to the new WPF model, I have not figured out how to update a
control in the midst of processing.

--------------
//non-complete example:

Button buGo;
TextBlock tbOutput;

private void buGo_Click( object sender, RoutedEventArgs e ) {
  int i;
  for (i=0; i < 1000; ++i) {
    tbOutput.Text = i.ToString();
}
--------------

After clicking on buGo, the program runs and then tbOutput changes to '999'.
What I want is to see each individual number '0' ... '999' in tbOutput after
each modification, even if I have to make a fct call manually after setting
tbOutput.Text.

In System.Windows.Forms, I could call tbOutput.Refresh() which would
force the control to re-render itself immediately.

I am having no luck finding a corollary in the WPF and am guessing that
some sort of threading solution may be the only way to accomplish this,
meaning that I may have to split out my processing into a second thread,
which makes an 'Invoke'-ish type call back to the original thread that
created the controls.

Thanks in advance,
Robert McCall

PS: *MODERATORS* Might I suggest 3 new .NET sub-categories:  WPF, WCF and WF/WWF?
   Now that Microsoft has officially released .NET 3.0, these questions are gonna start flooding in, maybe.
0
Comment
Question by:mihrlogic
  • 2
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18010526
Does Application.DoEvents help?
0
 

Author Comment

by:mihrlogic
ID: 18010622
AlexFM,

No, there is no such call in the WPF ~ Windows Presentation Foundation.  It's a completely
different model; and, FYI, Application.DoEvents() exposes your program to all kinds of problems
with re-entrancy, so it is better to use Obj.Refresh() instead.

Thanks anyway,
Robert
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 18010691
Try Dispatcher.UpdateRender(). In any case, you need to add some kind of sleep to this code to see that something changes.
Of course, threading is better solution. WPF has some kind of Invoke methods which allow to talk with WPF controls from worker threads - exactly like in Windows Forms.

In cheap DoEvents solution, reentrance problems are solved by disabling some UI elements. This is some information about WPF DoEvents:
http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!411.entry?_c11_blogpart_blogpart=blogview&_c=blogpart#postcns!FD9A0F1F8DD06954!411
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why, and when, to use Windows Workflow 1 31
Tool Box 2 31
VB.Net How to Exit Sub - Exit Form??? 5 47
SQL Exceptions 3 35
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now