Visual Studio
(1)
WPF
(1)
Application
(1)
BeginInvoke
(1)
GetString
(1)
Control
(1)
Button
(1)
Thread
(1)

[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

Hi and welcome!From which thread do you call the OutputA method?

Asked By kucer
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

[newbie] Text Control not updating

Asked By Matte
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".

You can see the active threads in Visual Studio in the Threads window.

Asked By kucer
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
[newbie] Text Control not updating
Asked By Matte
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.
Hi, glad you find how it works.
Asked By kucer
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
[newbie] Text Control not updating
Asked By Matte
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
Post Question To EggHeadCafe