473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BeginInvoke and callbacks

Hello,

If I understand BeginInvoke correctly, when it is called your delegate is
run on a thread pool thread. Now, if you supplied a callback delegate, that
too is called on the same thread pool thread.

My question is this: Do I ever need to check the value of InvokeRequired in
my callback method before working with some GUI controls? Won't it always be
required since the callback is running on a thread pool thread? I see some
code examples of asynch calling and the examples usually have an
InvokeRequired check in the callback before doing any GUI work. It just
seems that that call wouldn't be necessary.

Thanks.
Oct 18 '06 #1
2 3812

Flack wrote:
Hello,

If I understand BeginInvoke correctly, when it is called your delegate is
run on a thread pool thread. Now, if you supplied a callback delegate, that
too is called on the same thread pool thread.

My question is this: Do I ever need to check the value of InvokeRequired in
my callback method before working with some GUI controls? Won't it always be
required since the callback is running on a thread pool thread? I see some
code examples of asynch calling and the examples usually have an
InvokeRequired check in the callback before doing any GUI work. It just
seems that that call wouldn't be necessary.
I think that the call is there just for the sake of flexibility. If
some future modification calls the callback method on the UI thread,
then this avoids an attempt to Invoke to the UI thread when it's not
needed.

BTW, I believe that you have the idea of BeginInvoke / Invoke backward.
You use the Thread class or QueueUserWorkIt em to start a delegate
running on a background thread. If that delegate calls its callback
delegate using Invoke or BeginInvoke then the callback delegate runs on
the UI thread. If, on the other hand, that delegate calls its callback
delegate directly, and the callback delegate wants to manipulate UI
controls, it must in turn invoke another method using Invoke or
BeginInvoke.

In other words:

1. Code in UI thread invokes delegate to run in the background using,
say, QueueUserWorkIt em.
Then, either:
2a. Background method completes and invokes callback delegate using
Invoke or BeginInvoke.
3a. Callback delegate runs on UI thread.
Or:
2b. Background method completes and calls callback delegate directly.
3b. Callback delegate calls another method using Invoke or BeginInvoke
4b. That method runs on UI thread.

You might want to read Jon Skeet's excellent article on threading.
There's a section on threading in Windows Forms:

http://www.yoda.arachsys.com/csharp/threads/

As well, I'm sure that if I've given you bad information, someone
(probably Jon) will jump in and correct me... :-)

Oct 18 '06 #2
You should really separate Delegate.BeginI nvoke or Control.BeginIn voke;
it isn't clear which you mean (I'm reading it as Delegate.BeginI nvoke).

Delegate.BeginI nvoke (i.e. BeginInvoke called on a delegate instance)
pushes the work onto a background thread (similar to ThreadPool or
Thread usage) - however Control.BeginIn voke pushes the work to the UI
thread.

As for "is it necessary", first recall that callbacks aren't the only
issue here; some event handlers (etc) can fire on any thread - e.g
System.Timers.T imer.

A lot of code tends to do something like:

void SomeMethod([some signature]) {
if(InvokeRequir ed) {
// Invoke/BeginInvoke to SomeMethod
} else {
// do the actual work
}
}

So actually, SomeMethod gets called twice; once on a pool thread, once
on the UI. So yes, in this case it is absolutely necessary. If is also
useful to help shortcut when things are happening on the UI thread; in
a highly threaded environment it can be useful to be thread-aware
(agnostic?), so that *however* it happens, it works.

Marc

Oct 18 '06 #3

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

Similar topics

5
3247
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and object members. Can anyone help? I'm writing a C++ wrapper for a fairly old programming interface to a document editing program that has no OOP whatsoever; only tons of structs. This program has different callbacks I'm supposed to implement for...
6
5586
by: arkam | last post by:
Hi, I found a sample on internet : formMain.BeginInvoke(new MyClickHandler(this.OnMyClick)); I would like to do the same but in a class library where there is no forms ! Where can I find a BeginInvoke equivalent for a class library ?
9
7600
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback function of my form follows the standard design pattern: if(IsDisposed){ return; }
2
2772
by: Nick Palmius | last post by:
Hello, I am experimenting with async callbacks using BeginInvoke and EndInvoke, and although my code which I have shown below works, when the program stops at the end (on the ReadLine()), there are still 2 threads running if i pause the execution, even if I kow that EndInvoke has been called on the thread. This also happens in the async callback sample on msdn. Is this anything I need to wory about, because it doesn't seen too good to...
6
15159
by: Valerie Hough | last post by:
I'm not entirely sure what the difference is between these two approaches. In order to avoid reentrant code, I was using Control.BeginInvoke in my UI to cause an asynchronous activity to be done on the UI's message loop. I began to get System.ExecutionEngineException errors so (on the theory of do something different if what you're doing isn't working) I switched to using delegate.BeginInvoke with the appropriate EndInvoke and the problem...
9
5667
by: john doe | last post by:
I have a question about BeginInvoke method found on the Control class. To quote the docs: "Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on." Which is fine, but I'm wondering how does this method get called asynchronously if it's on the same thread we are working on? Surely it blocks the thread until returned?
3
6053
by: RWF | last post by:
I have a windows form of which I will be saving the user's selections from DropDownBoxes, CheckBoxes, and RadioButtons. Once the choices are collected, I really will have no use for any of the controls. Is there a benefit of using Control.BeginInvoke opposed to ThreadPool.QueueWorkItem? From the threads I have briefly scanned over on the www, there really is no difference other. Some have mentioned that ThreadPool.QueueWorkItem maybe...
5
2487
by: GT | last post by:
Could someone please explain the difference of (refer to example code below) d.BeginInvoke("some text", null,null); //Alternative A and BeginInvoke( d, new object { "some text" } //Alternative B Both calls causes asynchonous execution to begin at the address specified by the delegate, and both calls returns an IAsyncResult, isn't that so? However I've experienced that these calls can cause different program behaviour....
2
4424
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
0
9591
marktang
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...
0
10225
Oralloy
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...
0
10053
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 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...
0
9867
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 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...
1
7415
isladogs
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...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
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...

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.