473,387 Members | 1,721 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,387 software developers and data experts.

Problem with various BeginInvoke() calls

GT
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.
Someone please explain.

**********Example code:******************
public delegate void SomeDelegate(string s);
private void SomeDelegateMethod(string s)
{
//Do whatever with the string
}

private void SomeOtherMethod()
{
SomeDelegate d = new SomeDelegate(SomeDelegateMethod);
//Alternative A:
d.BeginInvoke("some text", null,null);
//Alternative B:
BeginInvoke( d, new object[] { "some text" }
}
************************************
Aug 29 '06 #1
5 2469
The form's .BeginInvoke will either push the work onto the UI thread (via
the event queue), or do it immediately (on the current thread) if it is
/already/ on the UI thread; the delegate's .BeginInvoke will perform the
work on a background (pool) thread.

If the underlying method attempts to work with UI controls then the
delegate's BeginInvoke will lead to exceptions due to thread affinity. If
you rely on e.g. opening a gate (semaphore, manualresetevent or whatever)
after the .BeginInvoke, then this may deadlock (itself, unusually) with the
form's approach (since the thread will never reach the code to open the
gate).

Marc
Aug 29 '06 #2
GT wrote:
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
This second BeginInvoke isn't a complete statement, BTW.

The two extra arguments to BeginInvoke (that are both null in your first
example) are (1) the delegate to call when the operation is complete
(AsyncCallback) and (2) the argument to pass as the value of the
AsyncState property on the IAsyncResult passed to the AsyncCallback in
(1).

The AsyncCallback pattern basically allows asynchronous programming
through manual conversion of code into continuation-passing style. The
idea is that you chain your operations through callbacks, so that each
operation calls an asynchronous method Begin*, passes in a callback
method, and the callback method calls the corresponding End* and itself
finishes in a Begin* (or not, if the whole task is finished).

-- Barry

--
http://barrkel.blogspot.com/
Aug 29 '06 #3
Sorry - I meant to be clearer : the self-blocking scenario can only apply
when you call the form's .BeginInvoke from the UI thread itself...

i.e. (pseudo# ;-p) here we will probably stop at WaitForGate() for quite
some time...

ManualResetEvent gate = {blah};
void MainMethod() {
iar = BeginInvoke(SomeFunc);
gate.Set();
iar.EndInvoke();
}

void SomeFunc() {
gate.WaitOne();
// do something interesting
}
Aug 29 '06 #4
GT wrote:
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.
Someone please explain.

**********Example code:******************
public delegate void SomeDelegate(string s);
private void SomeDelegateMethod(string s)
{
//Do whatever with the string
}

private void SomeOtherMethod()
{
SomeDelegate d = new SomeDelegate(SomeDelegateMethod);
//Alternative A:
d.BeginInvoke("some text", null,null);
//Alternative B:
BeginInvoke( d, new object[] { "some text" }
}
************************************

The following link helped me to understand this issue:

http://www.codeproject.com/csharp/begininvoke.asp

--
You can email me directly by removing the NOSPAm below
xm**********@gmxNOSPAm.netNOSPAm
Aug 29 '06 #5
GT
Thanks. That article does the trick.
Not very obvious that there is such a difference though.

"Andreas Mueller" wrote:
The following link helped me to understand this issue:

http://www.codeproject.com/csharp/begininvoke.asp

Aug 29 '06 #6

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

Similar topics

1
by: Grandma Wilkerson | last post by:
My question concerns the documentation for Control.BeginInvoke(). At one point is says: "Executes the specified delegate asynchronously with the specified arguments, on the thread that the...
7
by: David Sworder | last post by:
Hi, I'm developing an application that will support several thousand simultaneous connections on the server-side. I'm trying to maximize throughput. The client (WinForms) and server communicate...
6
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...
4
by: Max | last post by:
Hi! In <MPG.1b660df23c2f8b6498af23@msnews.microsoft.com> Jon Skeet said that he _believes_ that for UI calls the WinForms team has _basically_ guaranteed that EndInvoke is not required after...
7
by: nick_tucker | last post by:
Is The following Code valid it is like a mini thread pool(a friend at work wrote it)? I have cut down the code a bit but kept the essential parts. The main part that I am would like explainedis...
2
by: Stampede | last post by:
Hi guys 'n' girls, I want to use callback methods when using BeginInvoke on some events. So far no problem, but know I thought about what could happen (if I'm not completly wrong). Lets say an...
9
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...
1
by: Thomee Wright | last post by:
I'm having a problem with a pair of applications I'm developing. I have a server application which interacts with several instruments, and a client app which connects to the server and provides a...
2
by: Flack | last post by:
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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,...
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...

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.