/// <summary>
 /// Processes the discover button click.
 /// </summary>
 private void Discover_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         RefreshMessage("Discovering...");
         UPnP.Discovery.Discover();
         UPnP.QueryDevice.QueryZoneAttributes();
         UPnP.QueryDevice.FindMasters();
         UPnP.QueryDevice.QueryZonePlayerXml();
         UPnP.QueryDevice.GetPlaylists();
         RefreshMessage((UPnP.Discovery.Zones.Count != 0) ?
                        "Discovered " + UPnP.Discovery.Zones.Count.ToString() +
                        " devices. (" + DateTime.Now.ToString() + ")" : "No devices discovered. Try discovery again.");
         foreach (string zone in UPnP.Discovery.Zones)
         {
             Uri      uri = new Uri(zone);
             ZoneData zd  = new ZoneData
             {
                 ZoneName    = UPnP.Discovery.ZoneTable[zone],
                 ZoneAddress = uri.Host,
                 ZoneType    = UPnP.Discovery.ZoneTypes[zone],
                 ZoneID      = UPnP.Discovery.ZoneIDs[zone],
                 ZoneMaster  = UPnP.Discovery.ZoneMasters[zone].ToString()
             };
             if (!_ZoneCollection.Contains(zd, new ZoneComparer()))
             {
                 _ZoneCollection.Add(zd);
             }
             if (UPnP.Discovery.ZoneMasters[zone] && !_MasterZones.Contains(zd, new ZoneComparer()))
             {
                 _MasterZones.Add(zd);
             }
         }
         foreach (KeyValuePair <string, string> kvp in UPnP.QueryDevice.Playlists)
         {
             PlaylistData pd = new PlaylistData
             {
                 PlaylistName = kvp.Value,
                 PlaylistSQ   = kvp.Key,
                 NumItems     = UPnP.QueryDevice.GetPlaylist(kvp.Key, UPnP.QueryDevice.PlaylistAction.Count, 0 /* not used for count */)
             };
             if (!_PlaylistCollection.Contains(pd, new PlaylistComparer()))
             {
                 _PlaylistCollection.Add(pd);
             }
         }
     }
     catch (Exception ex)
     {
         RefreshMessage(ex.Message + " Try discovery again.");
     }
 }
        /// <summary>
        /// Processes the discover button click.
        /// </summary>
        private void Discover_Click(object sender, RoutedEventArgs e)
        {
            ProgressBar pb = new ProgressBar();

            try
            {
                pb.Show();
                RefreshMessage("Discovering...");

                Task tsk = Task.Factory.StartNew(() => doDiscovery());     // run in the background
                Task.WaitAll(tsk);

                RefreshMessage((UPnP.Discovery.Zones.Count != 0) ?
                               "Discovered " + UPnP.Discovery.Zones.Count.ToString() +
                               " devices. (" + DateTime.Now.ToString() + ")" : "No devices discovered. Try discovery again.");
                foreach (string zone in UPnP.Discovery.Zones)
                {
                    Uri      uri = new Uri(zone);
                    ZoneData zd  = new ZoneData
                    {
                        ZoneName    = UPnP.Discovery.ZoneTable[zone],
                        ZoneAddress = uri.Host,
                        ZoneType    = UPnP.Discovery.ZoneTypes[zone],
                        ZoneID      = UPnP.Discovery.ZoneIDs[zone],
                        ZoneMaster  = UPnP.Discovery.ZoneMasters[zone].ToString()
                    };
                    if (!_ZoneCollection.Contains(zd, new ZoneComparer()))
                    {
                        _ZoneCollection.Add(zd);
                    }
                    if (UPnP.Discovery.ZoneMasters[zone] && !_MasterZones.Contains(zd, new ZoneComparer()))
                    {
                        _MasterZones.Add(zd);
                    }
                }
                foreach (KeyValuePair <string, string> kvp in UPnP.QueryDevice.Playlists)
                {
                    PlaylistData pd = new PlaylistData
                    {
                        PlaylistName = kvp.Value,
                        PlaylistSQ   = kvp.Key,
                        NumItems     = UPnP.QueryDevice.GetPlaylist(kvp.Key, UPnP.QueryDevice.PlaylistAction.Count, 0 /* not used for count */)
                    };
                    if (!_PlaylistCollection.Contains(pd, new PlaylistComparer()))
                    {
                        _PlaylistCollection.Add(pd);
                    }
                }
            }
            catch (Exception ex)
            {
                RefreshMessage(ex.Message + " Try discovery again.");
            }
            finally
            {
                if (pb.IsLoaded)
                {
                    pb.Close();
                }
            }
        }