//private static int ThreadCount = 0; private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { var argument = (ConversionTaskParameters)e.Argument; BackgroundWorker backgroundWorker = argument.BackgroundWorker; backgroundWorker.ReportProgress(0, "Waiting in queue"); semaphore.Wait(); //while (ThreadCount>=2) //{ // backgroundWorker.ReportProgress(0,"Waiting in queue"); // Thread.Sleep(5000); //} ////ThreadCount++; //Interlocked.Increment(ref ThreadCount); //this.CreateDownloadFolder(); if (argument.ConversionProfile != null) { string fileNameWithoutExtension; string uRL; string str3; if (argument.OriginalFileLocation == null) { throw new Exception("Incorect video location specified"); } var result = new VideoResult(); bool flag = false; if ( Regex.Match(argument.OriginalFileLocation, @"(?:[Yy][Oo][Uu][Tt][Uu][Bb][Ee]\.[Cc][Oo][Mm]/((?:(?:(?:watch)|(?:watch_popup))(?:(?:\?|\#|\!|\&)?[\w]*=[\w]*)*(?:\?|\#|\!|\&)?v=(?<vid>-?[\w|-]*))|(?:v/(?<vid>-?[\w|-]*))))|(?:[Yy][Oo][Uu][Tt][Uu].[Bb][Ee]/(?<vid>-?[\w|-]*))") .Success) { string str4; backgroundWorker.ReportProgress(0, "Determining location of the video stream"); var u = new YouTubeDownloader.YouTubeDownloader(); ResourceLocation location = u.ResolveVideoURL(argument.OriginalFileLocation, argument.QualityIndex, argument.ConversionProfile.PreferedType, out str4); if ((location == null) || string.IsNullOrEmpty(location.URL)) { throw new Exception("Unable to obtain initial information about this video"); } flag = ((argument.ConversionProfile.PreferedType != YouTubeDownloader.YouTubeDownloader.VideoStreamTypes.Any) && !string.IsNullOrEmpty(argument.ConversionProfile.AlternativeConversionString)) && (location.StreamType != argument.ConversionProfile.PreferedType); str3 = !flag ? argument.ConversionProfile.ConversionStringTemplate : argument.ConversionProfile.AlternativeConversionString; uRL = location.URL; fileNameWithoutExtension = TextUtil.FormatFileName(str4); result.Title = str4; } else { fileNameWithoutExtension = Path.GetFileNameWithoutExtension(argument.OriginalFileLocation); uRL = argument.OriginalFileLocation; result.Title = fileNameWithoutExtension; result.FileSize = new FileInfo(argument.OriginalFileLocation).Length; str3 = !string.IsNullOrEmpty(argument.ConversionProfile.ConversionStringTemplate) ? argument.ConversionProfile.ConversionStringTemplate : argument.ConversionProfile.AlternativeConversionString; } backgroundWorker.ReportProgress(0, "***" + fileNameWithoutExtension); string str5 = string.Format("{0}.{1}", fileNameWithoutExtension, argument.ConversionProfile.OutputExtension); string str6 = DateTime.Now.Ticks.ToString(); string targetTmpFile = Path.Combine(ApplicationSettings.Instance.DefaultDownloadFolder, str6); string targetFile = Path.Combine(ApplicationSettings.Instance.DefaultDownloadFolder, str5); if (File.Exists(targetFile)) { backgroundWorker.ReportProgress(0, "Exist file:" + fileNameWithoutExtension); return; } long resultSize = 0L; result.ResultPath = targetFile; if (((argument.ConversionProfile.ConversionStringTemplate != null) || flag) && !argument.IndirectConversion) { try { e.Cancel = DownloadHelper.DownloadAndConvert(backgroundWorker, str3, uRL, targetFile, targetTmpFile, out resultSize); } catch (Exception exception) { resultSize = 0L; result.ResultException = exception; } } else { e.Cancel = DownloadHelper.InternalDownload(backgroundWorker, str3, uRL, targetFile, targetTmpFile, out resultSize); } result.FileSize = resultSize; e.Result = result; try { DownloadSubtitle(uRL, fileNameWithoutExtension); } catch (Exception ex) { //todo } } }