question

aa-9375 avatar image
0 Votes"
aa-9375 asked XingyuZhao-MSFT commented

How to update UI periodically without Forms.Timer

I have a question!
Is it bad that update UI in ther timer_tick()? (update UI example: change label1.Text)
Without timer, I should use Invoke() in other thread(made by me).
I want to ask this: using Invoke periodically is better than update UI in timer_tick()?

I think the work of timer is short enough to complete within the timer interval, but my boss hates it.

I'm using .Net Framework 4.5, winform

dotnet-runtime
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered aa-9375 commented

Timer_Tick is based on WM_TIMER system message. Control.Invoke is also based on a message (a custom one, according to sources of Control).

However, WM_TIMER is a low-priority message (as well the paint message), therefore refreshes of your UI will be postponed if there are more important messages to consider, which seems to be a good idea. Timer looks appropriate.

But timer is invoked periodically, unconditionally, while Control.Invoke is called explicitly, only when UI needs updates (although, you did not give enough details about the timer). Control.Invoke looks more appropriate.

You can evaluate the complexity of Control.Invoke and Control.BeginInvoke since the sources are public.

Even if the code of Control.Invoke seems untrivial, maybe your conclusion will be: to use Control.Invoke especially because the Boss insists, but continuing the evaluation of Timer_Tick in other circumstances.




· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I decided to use BeginInvoke.
In my system, there's a thread that read data from our machine and process it.
The data is save on static global array var so that timer_tick uses it to visualize on window.
My boss worried that timer's work is too much to finish its work untill next timer_tick is invoked.
He told me that timer must work do only simple works like cont++, so that timer can finish it early and not disturb other threads.

0 Votes 0 ·

I think BeginInvokecan disturb other threads' work as if timer does.

You mean timer_tick beging invoked and BeginInvoke work similar and It's no matter whatever I use??
Did I understand right?? ToT

  1. using timer
    void timer_tick(){
    label.Text="hello";
    }

  2. no using timer void
    DoThread(){
    Thread.sleep (1000);
    form1.BeginInvoke(delegateMethodToChangeLabelText);

}





0 Votes 0 ·
Castorix31 avatar image
1 Vote"
Castorix31 answered

The Windows.Forms.Timer events are raised on the UI thread and it is the simplest method to update controls without invoke,
as shown in MS samples like How to: Run Procedures at Set Intervals with the Windows Forms Timer Component


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.