get notification when all async calls complete

  • Thread starter Thread starter Brian C
  • Start date Start date
B

Brian C

I'm writing my first asynchronous application ... spidering a website.
Each call to process a page records the page info and recursively and
asynchronously calls the routine to spider the (internal) links on
that page.

I'm currently writing the pages (titles, urls, etc) into an
arraylist. Then when the spidering is done I will use the arraylist
info in the next part of the project.

How do I detect when all the spidering is done and I can move on to
the next step?
 
For the interest of those wondering.

I defined a simple int, then incremented by 1 when starting a new
thread, decremented on completion/termination. And I tested
appropriately to see if the value was zero. When it reached zero I
initiated the closing routines.

I had simply been wondering if there was a built in solution to this.
 
You will run into all sorts of problems when you let multiple threads
update/read the same variable without thread synchronisation. For
example, thread A could read the value (in order to increment it),
then Thread B reads the value (in order to increment it), then both
threads increment their copy of the value then each thread writes the
value back, they will both have incremented it to the same number.
What you will find is that sometimes your code will work and sometimes
it wont. Take a look at InterlockedIncrement and InterlockedDecrement,
as these should be enough for what you are doing. I'd recommend
reading a few articles on multithreading as well so that you get an
idea of what sort of things work and what sort of things to avoid.

Matt
 
Thanks Matt,

I've just started running into the oddities you describe (different
results different times). It's good to know what to read next.

My first foray into windows forms from the world of the web is proving
interesting.

Brian
 
Back
Top