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

EndInvokeCalled

I came across the following sample code:

while (!ar.EndInvokeCalled)
{
// If you comment out the line below, the loop will not work
// because you have to let the Form process events in the message
queue
Application.DoEvents();
}

I am not using Forms and a tight loop still does not work. Why is that?
What is the correct way to check this property in the parent thread?
Please note that I am not trying to eliminate the busy wait code.

Thanks

Nov 17 '05 #1
5 1422
marvind,

It seems like you are waiting for your asynchronous operation to
complete. Instead of polling like that, why not just pass a callback to
your call to BeginInvoke, which is called when the operation is complete?
Then, you can complete your operation from there.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I came across the following sample code:

while (!ar.EndInvokeCalled)
{
// If you comment out the line below, the loop will not work
// because you have to let the Form process events in the message
queue
Application.DoEvents();
}

I am not using Forms and a tight loop still does not work. Why is that?
What is the correct way to check this property in the parent thread?
Please note that I am not trying to eliminate the busy wait code.

Thanks

Nov 17 '05 #2
Yes, in fact, I pass a callback and have made the call to EndInvoke the
very last piece of code in the callback (I do not need results). Still
I have the situation that the parent thread cannot exit after spawning
the threads and it has nothing else to do, so the parent thread waits
until timeout and then signals currently spawned threads to exit (by
setting a boolean variable to true that is checked by spawned threads)
and then waits for them to exit by polling EndInvokeCalled for each
currently spawned thread. When EndInvokeCalled is true for all
currently executing threads, the parent thread exits.

I am not sure if I have used the best design but here are my thoughts:
1. I wanted to spawn an initial set of threads one on each server in
the parent and then have the spawned threads finish their work and
start a new one async on the same server and then exit
2. I wanted a realiable way to know if the threads had finished. Apart
from using other signaling mechanisms like ManualResetEvent or
semaphores, I could only think of the EndInvokeCalled property.
2. End

Nov 17 '05 #3
marvind,

You should call EndInvoke at the start of the callback, even if you
don't need the results. The reason for this is that EndInvoke will
(ultimately) release the mutex that is associated with the implmentation of
IAsyncResult. Right now, it doesn't, but it should be fixed at a later date
(the CLR team knows about this, and it is slated for fixing in the next
version of .NET beyond 2.0). This will make your code more tolerant for
future changes.

That being said, if you have access to the IAsyncResult implementation,
instead of polling for EndInvokeCalled, why not just wait on the
AsyncWaitHandle that is exposed? If you are doing this in a UI thread, then
you can spawn a new thread which will wait on all of the callbacks.

Or, you could have a single callback method which is the callback for
all of the methods you call. You then keep a list of the asycnhronous
operations somewhere which you remove from the list when the operation is
complete.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Yes, in fact, I pass a callback and have made the call to EndInvoke the
very last piece of code in the callback (I do not need results). Still
I have the situation that the parent thread cannot exit after spawning
the threads and it has nothing else to do, so the parent thread waits
until timeout and then signals currently spawned threads to exit (by
setting a boolean variable to true that is checked by spawned threads)
and then waits for them to exit by polling EndInvokeCalled for each
currently spawned thread. When EndInvokeCalled is true for all
currently executing threads, the parent thread exits.

I am not sure if I have used the best design but here are my thoughts:
1. I wanted to spawn an initial set of threads one on each server in
the parent and then have the spawned threads finish their work and
start a new one async on the same server and then exit
2. I wanted a realiable way to know if the threads had finished. Apart
from using other signaling mechanisms like ManualResetEvent or
semaphores, I could only think of the EndInvokeCalled property.
2. End

Nov 17 '05 #4

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I came across the following sample code:

while (!ar.EndInvokeCalled)
{
// If you comment out the line below, the loop will not work
// because you have to let the Form process events in the message
queue
Application.DoEvents();
}

I am not using Forms and a tight loop still does not work. Why is that?
What is the correct way to check this property in the parent thread?
Please note that I am not trying to eliminate the busy wait code.

Thanks


a tight loop still does not work.....What exactly do you mean by this? Maybe
you should post some repro code.

Anyway, you should not call DoEvents() either, if there is no message loop
(no UI thread) this results in a NOP.

Willy.
Nov 17 '05 #5
Yes, I cannot call Application.DoEvents since it is not a UI thread. If I
have this tight loop (with call to DoEvents() commented out) then the thread
never exits the loop. If I add a bogus function DoSomeStuff() and put a wait
in that then the EndInvokeCalled property is updated. I did not know what was
happening (still do not) when I came across this article below (scroll to the
end or search for DoEvents):

http://www.knowdotnet.com/articles/d...printable.html
"Willy Denoyette [MVP]" wrote:

"marvind" <ma********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I came across the following sample code:

while (!ar.EndInvokeCalled)
{
// If you comment out the line below, the loop will not work
// because you have to let the Form process events in the message
queue
Application.DoEvents();
}

I am not using Forms and a tight loop still does not work. Why is that?
What is the correct way to check this property in the parent thread?
Please note that I am not trying to eliminate the busy wait code.

Thanks


a tight loop still does not work.....What exactly do you mean by this? Maybe
you should post some repro code.

Anyway, you should not call DoEvents() either, if there is no message loop
(no UI thread) this results in a NOP.

Willy.

Nov 17 '05 #6

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

Similar topics

6
by: Michael C | last post by:
Hello Can someone please tell me what I'm doing wrong? I'm writing an application that should be using callbacks to perform asynchronous calls to the Win32 API. Problem is it never reaches my...
0
by: marvind | last post by:
AysncResult handle is obtained by calling BeginInvoke. The calling thread can use members of AsyncResult such as IsCompleted or EndInvokeCalled. This object is updated asynchronously by another...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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:
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.