So I loop that will loop about 20000 times. Each time it loops, I create a new thread. The thread basically calls one method. The method is rather slow it takes four seconds to complete. It goes out and scrapes some page(which we have permission to scrape). I add a one second delay in the loop which would make sure only 4 pages are being scrapped at once. My question what happens to that thread once the method is completed?
Thread.Sleep(1000);
Thread t = new Thread(() => scraping(asin.Trim(), sku.Trim()));
t.Start();
It will get destroyed as soon as the method completes.
That being said, this is not an ideal approach. You should use the
ThreadPoolto avoid creating many threads.Instead of using
new Thread, consider usingThreadPool.QueueUserWorkItemto start off the task.In addition, if you’re using .NET 4, you can use
Parallel.ForEachto loop through your entire collection concurrently. This will use the ThreadPool (by default) to schedule all of your tasks.Finally, you probably should eliminate the
Thread.Sleepin your loop – it will just slow down the overall operation, and probably not provide you any gains (once you’ve switched to using the ThreadPool).