That's Cool
-
You have 256 tasks, which at least originally took several seconds, but reliably end, no matter if successful or not. They can be carried out independently without any data synchronization, you just have to go through the results afterwards. That screams for parallel processing. The conditions could not be better. I can't believe that this is a real surprise to an old trapper like you. Just out of curiosity: Did you really use 256 instances of a custom thread class or simply use the Parallel class, something like a Parallel.For() loop?
I did it this way:
//--------------------------------------------------------------------------------
public static void GetLanMachinesThreaded(System.Windows.Threading.Dispatcher dispatcher,
List<HostAddress> subnet,
Control control,
bool setItemsSource=false)
{
List<Task> tasks = new List<Task>();
foreach(HostAddress item in subnet)
{
var t = Task.Factory.StartNew(() => { item.FindHost(); });
tasks.Add(t);
}
Task.Factory.ContinueWhenAll(tasks.ToArray(), result =>
{
subnet.RemoveAll(item => item.Host == null || item.Shares.Count == 0);
if (control != null)
{
if (control is ItemsControl && setItemsSource)
{
dispatcher.BeginInvoke(new Action(() => ((ItemsControl)control).ItemsSource = subnet));
}
dispatcher.BeginInvoke(new Action(() => control.Focus()));
}
});
}The
item.FindHost
method searches for the IP's specified in thesubnet
parameter, and then looks for disk shares on hosts that were found. once that's completed, it removes shares that are specified in a filter list (default shares, and anything that isn't a disk share), and then removes any hosts that end up with no shares found. If the disks are spun up from recent access, it now takes about 25 seconds (because of the disk share enumeration). If the disks aren't spun up, it takes almost twice as long. Since this only happens when the user specifically wants to add a new shared media source, I think the performance is acceptable, and I'm not sure it could be made much (if any) faster.".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997