473,326 Members | 2,147 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,326 software developers and data experts.

BeginInvoke and Invoke

Hi all,

Are the two snippets below equivelent, since they both only continue
once the ShowForm method returns???

1.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
IAsyncResult asyncResult = this.BeginInvoke(del);
asyncResult.AsyncWaitHandle.WaitOne();
this.EndInvoke(asyncResult);

2.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
this.Invoke(del);
Thanks for any explaination
Nick
Nov 15 '05 #1
2 15505
They seem to be equivalent. But, in general, using BeginInvoke in such a
context ensures your worker thread wouldn't block if the main UI thread is
showing a modal window or is busy and cannot retrieve and dispatch messages
from the window message queue.

Also note that in Snippet #1 you can do some useful work after you've called
BeginInvoke and before you start waiting for the async call to complete.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Nick" <ni*********@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...
Hi all,

Are the two snippets below equivelent, since they both only continue
once the ShowForm method returns???

1.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
IAsyncResult asyncResult = this.BeginInvoke(del);
asyncResult.AsyncWaitHandle.WaitOne();
this.EndInvoke(asyncResult);

2.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
this.Invoke(del);
Thanks for any explaination
Nick


Nov 15 '05 #2
100
Hi Nick,

I assume we are talking about Control's BeginInvoke and Invoke methods.

1. If they are called form a thread different than the UI thread owning the
underlaying windows control handler
(for example they are called form a worker thread).
The effect of both will be the same. I belive the form with Invoke as
preferable because it is shorter and more readable. The form with
BeginInvoke is useful when you want to do some work between the momends of
calling the ShowForm method and polling the result.

2. When those two are called form the UI thread that owns the form they have
different behaviour:
- Using Invoke dosen't make sense. you can call the ShowForm method
directly.
- Using BeginInvoke in the same way like in your example will cause
deadlock. Because of the WaitOne() call.
Removing the line with WaitOne will bring us to the first case with invoke,
which is senseless.
BeginInvoke in the case of not polling the result could be verry useful if
you want to have PostMessage-like behaviour.

HTH
B\rgds
100
"Nick" <ni*********@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...
Hi all,

Are the two snippets below equivelent, since they both only continue
once the ShowForm method returns???

1.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
IAsyncResult asyncResult = this.BeginInvoke(del);
asyncResult.AsyncWaitHandle.WaitOne();
this.EndInvoke(asyncResult);

2.
delShowForm del = new delShowForm(ShowForm); //show form shows a form
this.Invoke(del);
Thanks for any explaination
Nick

Nov 15 '05 #3

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

Similar topics

2
by: Sandor Heese | last post by:
Question, When using BeginInvoke (on a From to marshal to the UI thread) with the EventHandler delegate I see some strange behaviour. The problem is that I call the UpdateTextBox method with a...
9
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...
3
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form {...
6
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...
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...
3
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...
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...
2
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...
7
by: Ben Voigt [C++ MVP] | last post by:
As much as the CLR team assures us that it's ok to fire-and-forget Control.BeginInvoke, it seems it isn't. Maybe this is a bug. See for example: the comments in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.