Example #1
0
 void DownloadThread()
 {
   lock (this) { runningThreads++; }
   try
   {
     string tempFile = Path.GetTempFileName();
     CompressionWebClient client = new CompressionWebClient();
     int index = -1;
     while (index < onlineFiles.Count && !cancel)
     {
       lock (this)
       {
         counter++;
         index = counter;
       }
       if (index >= onlineFiles.Count)
         return;
       string onlineFile = onlineFiles[index];
       bool success = false;
       try
       {
         client.DownloadFile(onlineFile, tempFile);
         var extCol = ExtensionCollection.Load(tempFile);
         lock (this)
         {
           MpeCore.MpeInstaller.KnownExtensions.Add(extCol);
         }
         success = true;
       }
       catch (Exception ex)
       {
         System.Diagnostics.Debug.WriteLine(string.Format("Error downloading '{0}': {1}", onlineFile, ex.Message));
       }
       Invoke((Action)(() =>
       {
         progressBar1.Value++;
         listBox1.Items.Add(string.Format("{0}{1}", success ? "+" : "-", onlineFile));
         listBox1.SelectedIndex = listBox1.Items.Count - 1;
         listBox1.SelectedIndex = -1;
       }));
       if (File.Exists(tempFile)) File.Delete(tempFile);
     }
   }
   catch { }
   finally
   {
     lock (this) 
     { 
       runningThreads--;
       if (runningThreads <= 0)
       {
         MpeCore.MpeInstaller.Save();
         Invoke((Action)(() =>
         {
           DialogResult = cancel ? DialogResult.Cancel : DialogResult.OK;
           Close();
         }));
       }
     }
   }
 }
    private void DownloadThumbnail()
    {
        try
        {
          if (!Directory.Exists(Package.LocationFolder))
            Directory.CreateDirectory(Package.LocationFolder);

          string url = Package.GeneralInfo.Params[ParamNamesConst.ONLINE_ICON].Value;
          if (url.IndexOf("://") < 0) url = "http://" + url;
          var client = new CompressionWebClient();
          byte[] imgData = client.DownloadData(url);
          var ms = new MemoryStream(imgData);
          var image = Image.FromStream(ms);
          img_logo.Image = image;
          string fileName = "";
          if (ImageFormat.Jpeg.Equals(image.RawFormat))
          {
            fileName = "icon.jpg";
          }
          else if (ImageFormat.Png.Equals(image.RawFormat))
          {
            fileName = "icon.png";
          }
          else if (ImageFormat.Gif.Equals(image.RawFormat))
          {
            fileName = "icon.gif";
          }
          else if (ImageFormat.Icon.Equals(image.RawFormat))
          {
            fileName = "icon.ico";
          }
          else if (ImageFormat.Bmp.Equals(image.RawFormat))
          {
            fileName = "icon.bmp";
          }
          if (!string.IsNullOrEmpty(fileName))
          {
            File.WriteAllBytes(Path.Combine(Package.LocationFolder, fileName), imgData);
          }
        }
        catch (Exception) { } // invalid url or image file or no internet connection
    }