.NET Framework - [newbie] Text Control not updating

Asked By Matte
17-Oct-08 05:22 AM
Hello everyone!
I'm just starting .NET programming now and I have to start from the .NET
Micro Framework (never had experience on the 'regular' one).

I wrote a small class on a DeviceSolutions Tahoe board with .NET MF 3.0 Beta
2 firmware.

I control the PWM in a for loop and watch for the results. The PWM DOES
respond (I see the avg ouput change with a multimeter), I see the code
running smoothly through Debug statements, but the Text control in the GUI is
not updating while I stay in the loop. It does update when I exit the loop,
but then I only see the last state I've been through, while I wanted it to
report on each change.
Here is the code:

public void OutputA()
{
Text text = ((Text)((Panel)MainWindow.Child).Children[0]);
Meridian.PWM modulator;
byte dutyCycle = 0;
uint frequency = 3 * 1000;

try
{
modulator = new Meridian.PWM();
for (dutyCycle = 0; dutyCycle < 101; dutyCycle += 10)
{
modulator.Configure(frequency, dutyCycle);
//Update the text, in my intention
text.TextContent =
Resources.GetString(Resources.StringResources.Running1) + frequency +
Resources.GetString(Resources.StringResources.Running2) + dutyCycle;

Debug.Print(Resources.GetString(Resources.StringResources.Running1) +
frequency + Resources.GetString(Resources.StringResources.Running2) +
dutyCycle);

System.Threading.Thread.Sleep(1 * 1000);
}
}
catch (Exception)
{
text.TextContent =
Resources.GetString(Resources.StringResources.Exception) + dutyCycle;
}
}

OutputA is a method of an Application, MainWindow is the only window I have,
MainWindow.Child is a Panel, MainWindow.Child.Children[0] is a Text control
initialized to a string.

It goes through the 10 iterations and the text remains the same all the
time. Then, after the last one, it updates (but I obviously see only the last
string I wrote).
As I understand, the GUI is updated in another thread, but I am sleeping a
sec for each iteration: I guess this is enough for the scheduler to switch to
the GUI thread and let it update the screen, so what's wrong with my code?

Thanks in advance for the help!

Matteo
Visual Studio
(1)
WPF
(1)
Application
(1)
BeginInvoke
(1)
GetString
(1)
Control
(1)
Button
(1)
Thread
(1)
  kucer replied...
17-Oct-08 01:40 PM
Hi and welcome!
From which thread do you call the OutputA method? Because if this is the GUI
one, it won't redraw while sleeping...  (and 3.0 will not allow you to set
TextContent property from non-GUI thread)

I would however recommend to use DispatcherTimer instead of the for cycle...
or do you have any specific reason for your design?
Jan
  Matte replied...
21-Oct-08 03:07 AM
I am calling from the main application thread, I thought the one that
managed GUI updates was a background one managed by the Framework. If this is
not the case, then the reason for the updates not taking place during Sleep()
becomes obvious.

Is the Main Thread also the GUI thread? Is there a reference on how this
works? I've been reading "Expert .NET Micro Framework", but it does not get
behind the scenes in relation to GUI code.
If you could point me to some online article on the GUI/Threading issue I
would be very grateful!

Talking about DispatcherTimer: there is no particular reason for this
design, this is just testing code I am writing to get used to the Framework.
However, I am interested in a non-periodic way of updating the UI from a
generic piece of code, so the DispatcherTimer is not what I'm after "in
general".
  kucer replied...
21-Oct-08 03:37 AM
You can see the active threads in Visual Studio in the Threads window.

The Application.Run() is a blocking call, how do you call OutputA from the
main thread then (in case you consider the entry thread as the main one)?

Anyway, when you get your data in another thread and want to change the UI,
you have to call the Dispatcher.BeginInvoke (or Dispatcher.Invoke) and do
the changes in a delegate.

You might want to check the
http://blogs.msdn.com/netmfteam/archive/2008/03/04/using-the-dispatcher.aspx
for an example.


Jan
  Matte replied...
21-Oct-08 09:37 AM
Ok, so I really missed some basic knowledge on .NET, I got it from this
article: http://msdn.microsoft.com/en-us/magazine/cc300429.aspx .

What I was doing, to answer your question about calling OutputA, is that I
had overridden Application.OnButtonUp like this:

protected void OnButtonUp(object sender, ButtonEventArgs e)
{
// Print the button code to the Visual Studio output window.
Debug.Print("Button " + e.Button + " pressed.");
switch (e.Button)
{
case Button.AppDefined1:
OutputA();
break;

//more code

So I guess OutputA was still executing in the entry thread, where the
Controls were instantiated. Thus, when I called Thread.Sleep() I was stopping
the entry/UI thread.

Now: suppose I am actually running in the same thread as the UI, how do I
change this code:

//Update the text, in my intention
text.TextContent = someString ;

System.Threading.Thread.Sleep( 1000);

So that the UI is actually updated BEFORE sleeping? I tried invalidating it,
but I obtained no update. Is there a method to force the a (synchronous?)
update?

Tell me if you think thihs is off topic, I understand this is not strictly
Micro Framework related.
  kucer replied...
21-Oct-08 05:26 PM
Hi, glad you find how it works. This is indeed more WPF related, but there
is no real solution how to wait for the repaint, as discussed in several
threads in this group as well (just some workarounds).

In the .NET MF 3.0, there is a new
Microsoft.SPOT.Presentation.WindowManager.PostRender event, which might tell
you when the rendering finished.

However, you should never ever put the UI thread into sleep... maybe you
could sleep somewhere else... ?

Jan
  Matte replied...
22-Oct-08 04:41 AM
Sure I could. I am just writing this to learn, so I have no constraints.
I thought the UI updates happened in a different thread managed by the CLR,
so I didn't mind sleeping in the entry thread. Now I see it's just not what
you want to do in most cases.

Thanks a lot for your help, I'll be giving back as soon as I am able! =)

Matteo
Create New Account
help
WPF, Visual Studio e aggiunta proprietà_automatiche .NET Framework Ciao, una domanda rapida. . .esiste un modo per evitare che Visual Studio 2008 (SP1 installato) decida di aggiungere le propriet? ai controlli di una Window nello XAML definito questa CheckBox Se mi sbaglio anche solo a spostarla di un pixel sulla Window, Visual Studio decide di far diventare lo XAML cos?: HorizontalAlignment = "Left" VerticalAlignment = "Bottom"> Prova< / CheckBox> Il che ogni volta devo eliminarle a mano. . . Ho provato a spulciare un po' le opzioni di Visual Studio, ma senza successo. Mi ? sfuggita qualche impostazione? Grazie mille :) - - soldati" Veterano di fanteria ad
newsgroup for vs wpf .NET Framework I am looking for the proper newsgroup to use for visual studio / wpf discussions. Gerry Visual Studio General Discussions Wpf (1) There appear to be no groups for WFP. I suggest you post your question web based forums. Here is an example: http: / / social.msdn.microsoft.com / Forums / en-US / wpf / threads / thanks P keywords: newsgroup, for, vs, wpf description: I am looking for the proper
ANN: Next Version of Visual Studio and .NET Framework .NET Framework PR: Visual Studio 2010 and .NET Framework 4.0 Overview - - Thomas Scheidegger - 'NETMaster' http: / / dnetmaster.net / .NET Framework - German Discussions Visual Studio 2010 (1) Visual Studio (1) Silverlight (1) XAML (1) WPF (1) ISVs (1) WinForms (1) Entwicklungen (1) Hallo, Berauschend
Expression Blend 2 using wrong version of Visual Studio .NET Framework Hi, If I create a WPF project and Blend 2 (Service pack 1 installed) and go to the properties tab to add an event to the code behind file, Blend starts the wrong version of Visual Studio. I have BOTH Visual Studio 2008 & 2005 installed. Expression Blend 2 tries to start Visual Studio 2005 and the throws an exception (Cannot change the code-behind file. Operation abported
WPF e Visual Studio 2005 .NET Framework Ciao, mi confermate che non esiste piu' l'extension WPF per Visual Studio 2005? Potrei magari trovarla da qualche altra parte? Grazie mille Ciao BlackSun VB.NET - Italian Discussions Visual Studio 2005 (1) Office 2007 (1) Office 2003 (1) UGIdotNET (1) Excel (1) WPF (1) Report