473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

UI and worker thread

Hi

My main UI thread needs to create a worker thread. To signal that the
work is finished the UI should be updated.

I'm worried about that worker thread calling back via a delegate when
it has finished its work. UI stuff is message-queue based and handled
by a single thread spinning the standard message loop. But the worker
thread calling back is obviously a different thread and, of course,
isn't going to be 'serialized' by the message queue in any way.

So, how best to do update the UI safely?

Hope this makes sense.

Emma Middlebrook
Nov 16 '05 #1
8 4488
emma middlebrook <em**************@fastmail.fm> wrote:
My main UI thread needs to create a worker thread. To signal that the
work is finished the UI should be updated.

I'm worried about that worker thread calling back via a delegate when
it has finished its work. UI stuff is message-queue based and handled
by a single thread spinning the standard message loop. But the worker
thread calling back is obviously a different thread and, of course,
isn't going to be 'serialized' by the message queue in any way.

So, how best to do update the UI safely?


Use Control.Invoke - that makes sure the specified delegate is called
in the UI thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
emma middlebrook <em**************@fastmail.fm> wrote:
My main UI thread needs to create a worker thread. To signal that the
work is finished the UI should be updated.

I'm worried about that worker thread calling back via a delegate when
it has finished its work. UI stuff is message-queue based and handled
by a single thread spinning the standard message loop. But the worker
thread calling back is obviously a different thread and, of course,
isn't going to be 'serialized' by the message queue in any way.

So, how best to do update the UI safely?


Use Control.Invoke - that makes sure the specified delegate is called
in the UI thread.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet


Hi,

Invoke will indeed 'serialize' it. A call to Invoke will block until the
delegate is executed on the UI thread.

BeginInvoke on the other hand will post the task of invoking the delegate in
the thread's queue and then return. The delegate will be invoked as soon as
the UI thread sees fit (queue based). This prevents the worker thread from
blocking, and allows asynchronous execution. A call to EndInvoke will block
until the delegate was invoked and returns results if the delegate returns
results. The call to Endvoke isn't necessary if the delegate doesn't return
results.

These are all methods of the ISynchronizeInvoke interface. The Control class
implements this interface.

http://msdn.microsoft.com/library/de...classtopic.asp

Cheers,
---
Tom Tempelaere
Nov 16 '05 #3
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotmail.com|/\|APS0_N_>> wrote:
BeginInvoke on the other hand will post the task of invoking the delegate in
the thread's queue and then return. The delegate will be invoked as soon as
the UI thread sees fit (queue based). This prevents the worker thread from
blocking, and allows asynchronous execution. A call to EndInvoke will block
until the delegate was invoked and returns results if the delegate returns
results. The call to Endvoke isn't necessary if the delegate doesn't return
results.


Note that although the call to EndInvoke isn't necessary (at all, as
far as I'm aware, whether or not there's a return value) for
Control.BeginInvoke, you should *always* use it for other asynchronous
invocations - even if failing to call EndInvoke doesn't cause a leak
now, it's not guaranteed that it won't in the future. The UI guys have
apparently given assurances that Control.BeginInvoke can always be
"fire and forget" though.

It's not terribly difficult to write a wrapper which will make sure
EndInvoke is called for you if you want to avoid having to call it
yourself. There's a good sample that was posted on one of the mailing
lists - I'll find it if you're interested.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotmail.com|/\|APS0_N_>> wrote:
BeginInvoke on the other hand will post the task of invoking the delegate in the thread's queue and then return. The delegate will be invoked as soon as the UI thread sees fit (queue based). This prevents the worker thread from blocking, and allows asynchronous execution. A call to EndInvoke will block until the delegate was invoked and returns results if the delegate returns results. The call to Endvoke isn't necessary if the delegate doesn't return results.
Note that although the call to EndInvoke isn't necessary (at all, as
far as I'm aware, whether or not there's a return value) for
Control.BeginInvoke, you should *always* use it for other asynchronous
invocations - even if failing to call EndInvoke doesn't cause a leak
now, it's not guaranteed that it won't in the future. The UI guys have
apparently given assurances that Control.BeginInvoke can always be
"fire and forget" though.


The ISynchronizeInvoke.BeginInvoke and Control help don't mention, but you
can get around this by not having the delegate return results I guess. They
should have documented better.
It's not terribly difficult to write a wrapper which will make sure
EndInvoke is called for you if you want to avoid having to call it
yourself. There's a good sample that was posted on one of the mailing
lists - I'll find it if you're interested.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet


I don't see any direct use for me. Thanks.

Cheers,
---
Tom Tempelaere
Nov 16 '05 #5
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotmail.com|/\|APS0_N_>> wrote:
Note that although the call to EndInvoke isn't necessary (at all, as
far as I'm aware, whether or not there's a return value) for
Control.BeginInvoke, you should *always* use it for other asynchronous
invocations - even if failing to call EndInvoke doesn't cause a leak
now, it's not guaranteed that it won't in the future. The UI guys have
apparently given assurances that Control.BeginInvoke can always be
"fire and forget" though.


The ISynchronizeInvoke.BeginInvoke and Control help don't mention, but you
can get around this by not having the delegate return results I guess. They
should have documented better.


No, I believe even if the delegate doesn't return any results, you
still need to call EndInvoke in the general case in order to
*guarantee* that there won't be any leaks. Control.BeginInvoke is a
special case here, where you don't need to.

And yes, none of this is explicitly documented :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
0SP@|/\|t*******@hotmail.com|/\|@PS0_N_>> wrote:
No, I believe even if the delegate doesn't return any results, you
still need to call EndInvoke in the general case in order to
*guarantee* that there won't be any leaks. Control.BeginInvoke is a
special case here, where you don't need to.

So what you are telling is that the implementation of the
ISynchronizeInvoke interface for the Control class shows this
behaviour (not having to call EndInvoke for each BeginInvoke)? And
that this behaviour just isn't guaranteed for other implementers of
the ISynchronizeInvoke interface?
Exactly.
Anyway, it can be a real burden to have to call EndInvoke even for
delegates that don't return results. I personally don't call
EndInvoke when calling BeginInvoke on a Control, and I wouldn't use
BeginInvoke for a delegate that returns results (why would I?).
Well, you don't necessarily need the return value of the delegate, and
you can get the return value from EndInvoke if you need it anyway. I
would choose whether or not to call BeginInvoke based on my threading
requirements, not on the delegate's return type.
Unless you can have EndInvoke called behind the screens of course,
implicitly. Then the "Auto-EndInvoke" class you mentioned earlier
would come in handy.
Right.

http://msdn.microsoft.com/msdnmag/is...asicInstincts/
and
http://staff.develop.com/woodring/dotnet/#FireAndForget

may be of interest to you.
Maybe you could add it to your examples on your
website, I will be happy to check it out. I'm curious: does it use
another thread to finish things?
Not sure - I'll have to look more carefully when I have some time.
And yes, none of this is explicitly documented :(

Indeed, that is somewhat strange. Me :( too.


Interestingly, one of the webpages claims it *is* now documented, but I
can't see it in the docs for BeginInvoke or EndInvoke...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSBUZW1wZWxhZXJlKQ==?=" <_N_
0SP@|/\|t*******@hotmail.com|/\|@PS0_N_>> wrote: [...]
Maybe you could add it to your examples on your
website, I will be happy to check it out. I'm curious: does it use
another thread to finish things?


Not sure - I'll have to look more carefully when I have some time.

[...] --
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet


The workaround uses a thread from the threadpool to invoke the delegate,
using Delegate.DynamicInvoke. But I see no synchronization with the UI
thread.

---
Tom Tempelaere
Nov 16 '05 #8
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/> wrote:
The workaround uses a thread from the threadpool to invoke the
delegate, using Delegate.DynamicInvoke. But I see no synchronization
with the UI thread.


There doesn't need to be any synchronization with the UI thread -
Control.BeginInvoke doesn't require a workaround, because the UI team
have guaranteed that that particular method call won't leak.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark Hoffman | last post by:
All, My application spawns a worker process by calling BeginInvoke with an asynchronous callback method. This callback method is responsible for calling EndInvoke. No problems there; pretty much...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
5
by: Soren S. Jorgensen | last post by:
Hi, In my app I've got a worker thread (background) doing some calculations based upon user input. A new worker thread might be invoked before the previous worker thread has ended, and I wan't...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
0
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a...
4
by: dgleeson3 | last post by:
Hello all Yes I know its been done before, but something silly is killing me on this. I have the standard progress bar and worker thread scenario with progress of the worker thread being fed...
1
by: nicerun | last post by:
I'm using the Application_Start event at Global.asax.cs to invoke thread that do some job. I know that Application_Start event occurs when the very first request to Web Application received. -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.