15,891,184 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Use my saved
content filters
Ask a Question
All
Questions
All
Unanswered
FAQ
Best practice for updating UI in multi-threaded app
Please
Sign up or sign in
to vote.
4.50/5 (2 votes)
See more:
.NETCF
.NET
What is the best way to update many UI elements from many other threads, in a multi-threaded Windows desktop app?
Currently, I use delegates, which creates many temporary threads. Should the UI be on its own thread rather than the "main" thread? Would a queue pattern be better?
Thanks.
Posted
5-Jul-09 13:55pm
bobishkindaguy
Add a Solution
2
solutions
Top Rated
Most Recent
Please
Sign up or sign in
to vote.
Solution 1
Accept Solution
Reject Solution
Well, the UI thread
is
the main thread, so you can't really make it another thread. I would assume (but don't know for sure) that when you do Control.BeginInvoke or similar, it doesn't create another "temporary thread" since the whole thing with controls is that anything that changes them has to run
on the main thread
. As far as running a bunch of temporary threads, that's what the ThreadPool was designed to do, so using it should be just fine.
Permalink
Share this answer
Posted
5-Jul-09 16:03pm
Gideon Engelberth
Please
Sign up or sign in
to vote.
Solution 2
Accept Solution
Reject Solution
There is a world of difference between performing BeginInvoke on a delegate, and performing it on a control (passing a delegate as a parameter). IMHO, using the same name for both methods was a mistake, but what's done is done.
Performing BeginInvoke on a control will not create a new thread, nor take one from the thread pool, but will instead post a message in the control's UI thread's event queue. There is no particular limit to the number of messages that can be posted in the event queue, but one should nonetheless limit the number of unprocessed messages one will post to the event queue. Otherwise, if the UI thread gets blocked for some reason, such messages may accumulate without bound.
My particular coding style is to use an UpdateNeeded variable (declared as Integer, but functionally a Boolean) which is set to 0 at the beginning of the control's update routine. When I'm considering updating a control, I Threading.Interlocked.Exchange the variable to 1 and see if it wasn't already 1. If so, I perform a BeginInvoke on the control. If the BeginInvoke fails, I set the variable to 2 (so the control's update routine will know that the control needs updating, but other attempts to update the control will retry the BeginInvoke).
This style assumes that (1) if there are many changes to a control's value before an update takes place, all but the last should be ignored; (2) it is acceptable to update the fields associated with a control while it's being drawn, provided that one performs another update after the current one completes; (3) a redundant control update (e.g. because a field was changed after the update routine started to draw a control, but before the field's value was sampled) will be harmless; (4) updating controls every time the value changes will not cause excessive CPU loading.
If these assumptions hold, the code is fairly straightforward. If they do not, things are more complicated.
Permalink
Share this answer
Posted
6-Jul-09 13:13pm
supercat9
Add a Solution
Add your solution here
B
I
U
S
small
BIG
code
Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML
var
<
>
&
link
[^]
encode
untab
case
indent
outdent
OK
Paste as
Strip HTML
Encode HTML
Paste as-is
Code block
Quoted Text
Best guess
To display as
The content must be between 30 and 50000 characters.
Treat my content as plain text, not as HTML
Preview
0
…
Existing Members
Sign in to your account
...or Join us
Download, Vote, Comment, Publish.
Your Email
Password
Forgot your password?
Your Email
This email is in use. Do you need your
password
?
Optional Password
I have read and agree to the
Terms of Service
and
Privacy Policy
Please subscribe me to the CodeProject newsletters
Submit your solution
When answering a question please:
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Top Experts
Last 24hrs
This month
Pete O'Hanlon
250
Richard MacCutchan
85
OriginalGriff
35
merano99
10
Jo_vb.net
10
Pete O'Hanlon
465
OriginalGriff
230
Richard MacCutchan
135
merano99
100
KarstenK
50
Related Questions
Best practices on multithread updates to winform control
Best Practice for Wizard UI in MVVM.
Best practice for updating data - power query
Best practices on a couple of issues...
Adding react app in the web api solution is a good practice?
How to write update with best practice ?
Best practice for dynamic LINQ updates
Prevent thread starvation - best practices
Automatic update app
multi-threading database operations with C# (need help!)
CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900