how to re-draw controls on a Form at a regular interval?

G

Guest

Hello everyone,


I am using C# on a Pocket PC 2003 project based on .Net Compact Framework of
Visual Studio 2005. I want to re-draw some controls of a Form (Window) at a
regular interval (for example, change the title of some Label or something
similar). The issues I met with are,

1. My application has several Forms/Windows. How to check whether the
specific Form/Window (which I want to re-draw) is active? If the Form/Window
is not active, I think I should not re-draw the Form/Window. Am I correct?
Or, whether or not the Form/Window is active, I should always re-draw the
Form/Window?

2. Any code samples specific for C# of .Net Compact Framework?


thanks in advance,
George
 
P

Peter Duniho

George said:
[...]
1. My application has several Forms/Windows. How to check whether the
specific Form/Window (which I want to re-draw) is active?

Define "active".
If the Form/Window
is not active, I think I should not re-draw the Form/Window. Am I correct?

That depends on your definition of "active".
Or, whether or not the Form/Window is active, I should always re-draw the
Form/Window?

Probably. But since we don't know exactly what you mean by "active", it's
hard to say.
2. Any code samples specific for C# of .Net Compact Framework?

Assuming you use regular .NET components that are available in the Compact
Framework, you should not need examples specific to the Compact Framework.

As for the specific procedure, you might look into the Forms.Timer class.
This is probably the closest to what you'd want for causing a form to be
redrawn at a regular interval (though, IMHO you probably wind up just
changing the data for some control on the form at that regular interval and
let the regular redraw mechanism take care of the actual screen refresh).

Pete
 
C

Chan Ming Man

When doing .Net Compact Framework I would say try it with more devices if
you have. You will realize some behavour change with different devices I
bet.

chanmm
 
G

Guest

Thanks for your advice, Chan! Do you have any samples which meet with my
question?


regards,
George
 
G

Guest

Thanks Peter!
(though, IMHO you probably wind up just
changing the data for some control on the form at that regular interval and
let the regular redraw mechanism take care of the actual screen refresh).

Yes. This is just what I want. Do you have any samples to do this or close
to this question?


regards,
George


Peter Duniho said:
George said:
[...]
1. My application has several Forms/Windows. How to check whether the
specific Form/Window (which I want to re-draw) is active?

Define "active".
If the Form/Window
is not active, I think I should not re-draw the Form/Window. Am I correct?

That depends on your definition of "active".
Or, whether or not the Form/Window is active, I should always re-draw the
Form/Window?

Probably. But since we don't know exactly what you mean by "active", it's
hard to say.
2. Any code samples specific for C# of .Net Compact Framework?

Assuming you use regular .NET components that are available in the Compact
Framework, you should not need examples specific to the Compact Framework.

As for the specific procedure, you might look into the Forms.Timer class.
This is probably the closest to what you'd want for causing a form to be
redrawn at a regular interval (though, IMHO you probably wind up just
changing the data for some control on the form at that regular interval and
let the regular redraw mechanism take care of the actual screen refresh).

Pete
 
P

Peter Duniho

George said:
Yes. This is just what I want. Do you have any samples to do this or close
to this question?

Well, how are you displaying the data in the first place? I'm assuming you
have a form. Is the data displayed within the form by some control? Or do
you have some custom paint event handling code to draw the data directly
into the form?

For example, one easy way to display some text data in a form is to use some
kind of text control. Like a label or text box. Simply changing the Text
property of one of those controls is enough to cause the control itself to
be invalidated and force a redraw. You don't explicitly redraw...you just
change the data you want to change, and the related control and form handle
the rest for you.

Knowing more details about how you're actually displaying the data will help
in getting a good answer as to how to change that data in a manner
compatible with the usual Form redraw mechanisms.

Pete
 
P

Peter Duniho

George said:
The issue of your method is the text of some label is displayed only
passively -- but I need an auto-refresh approach. For example, if I open a
Form for a long time, and in this Form I want to display the updated
number
of new messages (like Outlook) -- the number of messages will increase
since
there are coming new messages, and this form needs to be refreshed at a
regular interval, other than passively upgrade the text.

Any comments or suggestions?

Every time you change the counter, you simply update the Text property of
the label. It will redraw automatically to reflect the new value.

If you would rather poll the counter on a regular basis (not too often, I
hope), then you can use a timer as I suggested earlier. In the timer, you
simply update the Text property of the label, and it will redraw
automatically to reflect the new value.

Pete
 
B

Bruce Wood

Peter said:
Every time you change the counter, you simply update the Text property of
the label. It will redraw automatically to reflect the new value.

Yes it will, IF whatever work you are doing that causes the counter to
change is being done on a background thread.

If you are doing all of the work on the UI thread and want to update
the Form to show progress then you are out of luck, unless you use
Application.DoEvents(), which is icky.

The trick is to do your work on a background thread. Have the
background job raise an event whenever the counter changes. In your
Form, subscribe to the event, and then use Invoke to call a method
under control of the UI thread again to update the counter.

You can read about multi-threading and Windows Forms on Jon Skeet's
site, here:

http://www.yoda.arachsys.com/csharp/threads/winforms.shtml
 
P

Peter Duniho

Bruce Wood said:
Yes it will, IF whatever work you are doing that causes the counter to
change is being done on a background thread.

Not necessarily. After all, one approach would be to process messages
intermittently (in response to network i/o events, a timer, etc.). A GUI
message pump will work just fine in that case.

But even if one ignores those options, one hopes that the OP has done
*something* to allow GUI events to occur while his processing is going on.
Otherwise, the OP has no usable UI at all. Why bother with forms at all, if
there is to be no user interaction?
If you are doing all of the work on the UI thread and want to update
the Form to show progress then you are out of luck, unless you use
Application.DoEvents(), which is icky.

No more icky than, for example, showing a modal dialog (which effectively
does the same thing).
The trick is to do your work on a background thread.

That is one trick, yes. There are other means to accomplish the end.
Have the
background job raise an event whenever the counter changes.

Or simply use Invoke to change the label's Text property, rather than
bothering with creating an event and adding a delegate to it.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top