suppose i want to download multiple files with task parallel library and now i want to visualize how many threads are working at the time of debugging from VS2010 IDE ? here is one code snippet which download many files using TPL.
var list = new[] { "http://google.com", "http://yahoo.com", "http://stackoverflow.com" }; var tasks = Parallel.ForEach(list, s => { using (var client = new WebClient()) { Console.WriteLine("starting to download {0}", s); string result = client.DownloadString((string)s); Console.WriteLine("finished downloading {0}", s); } });
please guide me or redirect me to any article from where i can acquire the knowledge to see how many thread is running to complete my task.
var tasks = Parallel.ForEach(list, s => { using (var client = new System.Net.WebClient()) { Console.WriteLine("starting to download {0}", s); client.DownloadStringAsync(new Uri(s)); noofthreads++; client.DownloadStringCompleted += client_DownloadStringCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; } }); int noofthreads = 0; private void client_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e) { throw new NotImplementedException();//here you can update UI with the progress } private void client_DownloadStringCompleted(object sender, System.Net.DownloadStringCompletedEventArgs e) { Console.WriteLine("finished downloading {0}", s); noofthreads--; //here you can update UI with the complete status }
try like this