Monrox
Power Member
- Apr 9, 2010
- 617
- 588
Your 'suggested' several ways are effectively diminishing the value of using multiple threads. Do you actually understand how threads work? Almost ANYTHING shared between threads SHOULD be LOCKED when a thread is accessing it, esp. if changes are to be made.Derpin' pretty hard there Monrox? I have no idea why you would suggest using multiple listboxes. There is several ways you can do this. With a background list, dictionary, or even a queue, that is being checked for items to add on the main thread.
You can also use .invoke or .begininvoke like people have already suggested. You won't get exceptions running on this model.
I'm seriously perplexed in what situation you would use 10 listboxes to cover multithreading. That seems redundant and arbitrary.
Otherwise we may allow for changes that will confuse the runtime. MS explicitly states that a multithreaded access to a shared object is not under the control of a regular developer. Like for example adding something to a dictionary; it may happen that a thread is adding a value up to the 5th byte, then another thread jumps in, drops 3 bytes of its value than the first thread comes back and pots the remaining stuff... you end up with gibberish. All such classes (or object instantiated thereof) are marked as thread unsafe in MSDN.
At least you know that this can be avoided with invoke etc, heck you can even disregard the danger by disabling cross thread warnings and pray nothing goes wrong.
The big BUT: when you go crazy on the locking, you are crippling the application speedwise. If the threads are finishing fast and thus need to constantly access the said dicitionary, it will result in a bottleneck. While a thread is doing its stuff there, all others will have to wait in line doing nada. Even worse, if a thread hangs, the whole application will lock hard. Of course you can say that a good developer should account for every worst case scenario but seeing how even hugely tested and retested programs like IE, FF and Chrome still lock from puny badly coded webpages it really is unfeasible.
The real power of multithreading is their independence and you want to tie them to a single resource (quote: a background list, dictionary, or even a queue). In my previous post I mentioned using text files to avoid such blocking bottlenecks. The harddrive is slow so yeah, that is not the best way to gain speed but then again you can use ram storage to offset that. Just didn't want to make the simple principles overcomplicated with an accompanying fluff.
Assuming you are interested to know how would I get the best speed in scraping: I would get a list of 1 million urls, split it into 1000 entries, create 1000 threads, assign each smaller list to a thread then start the threads making them save the results in some container of their own. When a thread has finished all the fetching, it will be allowed to unload its stuff into a global shared container. Yes locking it, but only once per thread during the whole execution process.
You on the other hand can go with a shared list, fetch urls one by one then update the global container, again one entry at a time, and when you are finally done in a week or so, come back and post whether you're still perplexed why using dedicated resources is better than using shared ones.
Last edited: