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 8 4523
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... 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.co m> 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 ISynchronizeInv oke interface. The Control class
implements this interface. http://msdn.microsoft.com/library/de...classtopic.asp
Cheers,
---
Tom Tempelaere
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotm ail.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.BeginIn voke, 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.BeginIn voke 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/ \|t*******@hotm ail.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.BeginIn voke, 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.BeginIn voke can always be "fire and forget" though.
The ISynchronizeInv oke.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.co m> http://www.pobox.com/~skeet
I don't see any direct use for me. Thanks.
Cheers,
---
Tom Tempelaere
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/
\|t*******@hotm ail.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.BeginIn voke, 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.BeginIn voke can always be "fire and forget" though.
The ISynchronizeInv oke.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.BeginIn voke is a
special case here, where you don't need to.
And yes, none of this is explicitly documented :(
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSB UZW1wZWxhZXJlKQ ==?=" <_N_
0SP@|/\|t*******@hotm ail.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.BeginIn voke is a special case here, where you don't need to.
So what you are telling is that the implementation of the ISynchronizeInv oke 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 ISynchronizeInv oke 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com... TT (Tom Tempelaere) <"=?Utf-8?B?VFQgKFRvbSB UZW1wZWxhZXJlKQ ==?=" <_N_ 0SP@|/\|t*******@hotm ail.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.co m> http://www.pobox.com/~skeet
The workaround uses a thread from the threadpool to invoke the delegate,
using Delegate.Dynami cInvoke. But I see no synchronization with the UI
thread.
---
Tom Tempelaere
TT (Tom Tempelaere) <"TT \(Tom Tempelaere\)" <_N_0SPA|/> wrote: The workaround uses a thread from the threadpool to invoke the delegate, using Delegate.Dynami cInvoke. But I see no synchronization with the UI thread.
There doesn't need to be any synchronization with the UI thread -
Control.BeginIn voke doesn't require a workaround, because the UI team
have guaranteed that that particular method call won't leak.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 straight out of MSDN.
My question is what happens when my worker thread raises an exception? I can
easily catch the exception, but I've noticed that unless my async callback
runs and EndInvoke gets called, then the worker thread never gets...
|
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 thread is aborted by the system.
How is one to keep the background thread from calling form.Invoke after the
form's window handle has been destroyed? This is definitely happening in my
application. I haven't read anything about this problem in...
|
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 waits
for the worker thread to complete by means of a while t.isAlive,
sleep(0) mechanism. But when my worker thread calls my
UpdateProgressBar routine, which calls Me.Invoke, the invoke call
blocks forever. But I can't figure out why the main...
|
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 a lock
pending, for example.
I want to be able to stop a thread temporarily, and then optionally resume
it or stop it for good.
|
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 entire operation. I could
request abort on the worker thread, but that is a) potentially messy, and b)
not guaranteed to take immediate effect anyway. I would rather have some way
of allowing the main thread to tell the worker thread that it...
| |
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 only one worker thread running
at any time (if a new worker thread start has been requested, any running
worker thread results will be invalid). I'm using the below method to invoke
a new worker thread, but when stress testing this I'm sometimes...
|
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 datagridview on the
form. I am using the following code to spawn the worker thread...
Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction));
WorkerThread.IsBackground = true;
WorkerThread.Start();
The problem I am having is...I cannot seem...
|
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 StopReader() method that simply aborts the worker thread (is
there a better way for the above situation?).
The StopReader creates an ObjectDisposedException when calling t.Abort(). WHY?
Public Sub StopReader()
|
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 back to the main UI and
displayed on the progress bar. I have a delegate and am using
BeginInvoke. But the progress bar is not updating.
|
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.
- Setting worker process count
1. Start | Programs | Administrative Tools | Internet Information
Services
2. Right-click on the application pool, e.g. ‘DefaultAppPool’, and
select ‘Properties’
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |