public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var users = _userManager.Users
                .DistinctBy(ChannelDownloadScheduledTask.GetUserDistinctValue)
                .Select(i => i.Id.ToString("N"))
                .ToList();

            var numComplete = 0;

            foreach (var user in users)
            {
                double percentPerUser = 1;
                percentPerUser /= users.Count;
                var startingPercent = numComplete * percentPerUser * 100;

                var innerProgress = new ActionableProgress<double>();
                innerProgress.RegisterAction(p => progress.Report(startingPercent + (percentPerUser * p)));

                await DownloadContent(user, cancellationToken, innerProgress).ConfigureAwait(false);

                numComplete++;
                double percent = numComplete;
                percent /= users.Count;
                progress.Report(percent * 100);
            }

            progress.Report(100);
        }
示例#2
1
文件: PostRequest.cs 项目: JDCB/JDCB
        protected override async Task FillDataTo(System.Net.WebRequest webrqst, CancellationToken cancellationToken, IProgress<ProgressReport> progress)
        {


            progress.ReportWhenNotNull(() => ProgressReport
                .CreateNew(RequestResponseSteps
                .Requesting_N_Uploading, 0, 1));
            var qd = System.Text.Encoding.UTF8.GetBytes(JsonClientHelper.GetQueryData(RequestData.PostFieldValues));

            webrqst.ContentType = "application/x-www-form-urlencoded";
#if SILVERLIGHT_4||SILVERLIGHT_5||DOTNET45

            webrqst.ContentLength = qd.Length;
    
#endif




            var rqstStrm = await webrqst.GetRequestStreamAsync().ConfigureAwait(false);
            await rqstStrm.WriteAsync(qd, 0, qd.Length).ConfigureAwait(false);
            progress.ReportWhenNotNull(() => ProgressReport
                .CreateNew(RequestResponseSteps
                .Requesting_N_Uploading, 1, 1));
        }
示例#3
1
        /// <inheritdoc/>
        public async Task<Uri> UploadMessageAsync(Stream content, DateTime expirationUtc, string contentType, string contentEncoding, IProgress<int> bytesCopiedProgress, CancellationToken cancellationToken = default(CancellationToken))
        {
            Requires.NotNull(content, "content");
            Requires.Range(expirationUtc > DateTime.UtcNow, "expirationUtc");

            string blobName = Utilities.CreateRandomWebSafeName(DesktopUtilities.BlobNameLength);
            if (expirationUtc < DateTime.MaxValue)
            {
                DateTime roundedUp = expirationUtc - expirationUtc.TimeOfDay + TimeSpan.FromDays(1);
                blobName = roundedUp.ToString("yyyy.MM.dd") + "/" + blobName;
            }

            var blob = this.container.GetBlockBlobReference(blobName);

            // Set metadata with the precise expiration time, although for efficiency we also put the blob into a directory
            // for efficient deletion based on approximate expiration date.
            if (expirationUtc < DateTime.MaxValue)
            {
                blob.Metadata["DeleteAfter"] = expirationUtc.ToString(CultureInfo.InvariantCulture);
            }

            blob.Properties.ContentType = contentType;
            blob.Properties.ContentEncoding = contentEncoding;

            await blob.UploadFromStreamAsync(content.ReadStreamWithProgress(bytesCopiedProgress), cancellationToken);
            return blob.Uri;
        }
            async Task<string> LoadContents(IResourceNetworkLayer networkLayer, IProgress<float> progress, CancellationToken token)
            {
                var reset = _cancelTokenSource.AddToken(token);
                if (_pageContentLoadPack == null || reset)
                {
                    lock(this)
                    {
                        if (_pageContentLoadPack == null || reset)
                        {
                            if (_originalUrl.Contains("&ajax=1"))
                                _pageContentLoadPack = Tuple.Create(networkLayer.Get(_originalUrl, _cancelTokenSource.Token, progress, null, false), progress);
                            else
                                _pageContentLoadPack = Tuple.Create(networkLayer.Get(_originalUrl + "&ajax=1", token, progress, null, false), progress);
                        }
                    }
                    
                }

                if (_pageContentLoadPack.Item2 != progress)
                {
                    HttpClientUtility.NetworkLayer.JoinProgress(_pageContentLoadPack.Item2, progress);
                }

                try
                {
                    return await _pageContentLoadPack.Item1;
                }
                finally
                {
                    _cancelTokenSource.Clear();
                }
            }
        public Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var items = _libraryManager.RootFolder.RecursiveChildren.ToList();

            var boxsets = items.OfType<BoxSet>().ToList();

            var numComplete = 0;

            foreach (var boxset in boxsets)
            {
                foreach (var child in boxset.Children.Concat(boxset.GetLinkedChildren()).OfType<ISupportsBoxSetGrouping>())
                {
                    var boxsetIdList = child.BoxSetIdList.ToList();
                    if (!boxsetIdList.Contains(boxset.Id))
                    {
                        boxsetIdList.Add(boxset.Id);
                    }
                    child.BoxSetIdList = boxsetIdList;
                }

                numComplete++;
                double percent = numComplete;
                percent /= boxsets.Count;
                progress.Report(percent * 100);
            }

            progress.Report(100);
            return Task.FromResult(true);
        }
示例#6
0
        public async Task InstallPackage(IProgress<double> progress, PackageVersionInfo package, CancellationToken cancellationToken)
        {
            // Target based on if it is an archive or single assembly
            //  zip archives are assumed to contain directory structures relative to our ProgramDataPath
            var isArchive = string.Equals(Path.GetExtension(package.targetFilename), ".zip", StringComparison.OrdinalIgnoreCase);
            var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename);

            // Download to temporary file so that, if interrupted, it won't destroy the existing installation
            var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
            {
                Url = package.sourceUrl,
                CancellationToken = cancellationToken,
                Progress = progress

            }).ConfigureAwait(false);

            cancellationToken.ThrowIfCancellationRequested();

            // Validate with a checksum
            if (package.checksum != Guid.Empty) // support for legacy uploads for now
            {
                using (var crypto = new MD5CryptoServiceProvider())
                using (var stream = new BufferedStream(File.OpenRead(tempFile), 100000))
                {
                    var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty));
                    if (check != package.checksum)
                    {
                        throw new ApplicationException(string.Format("Download validation failed for {0}.  Probably corrupted during transfer.", package.name));
                    }
                }
            }

            cancellationToken.ThrowIfCancellationRequested();

            // Success - move it to the real target 
            try
            {
                File.Copy(tempFile, target, true);
                //If it is an archive - write out a version file so we know what it is
                if (isArchive)
                {
                    File.WriteAllText(target+".ver", package.versionStr);
                }
            }
            catch (IOException e)
            {
                _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
                throw;
            }

            try
            {
                File.Delete(tempFile);
            }
            catch (IOException e)
            {
                // Don't fail because of this
                _logger.ErrorException("Error deleting temp file {0]", e, tempFile);
            }
        }
        /// <summary>
        /// Runs the specified progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var files = _libraryManager.GetAllIntroFiles().ToList();

            var numComplete = 0;

            foreach (var file in files)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    await RefreshIntro(file, cancellationToken).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error refreshing intro {0}", ex, file);
                }

                numComplete++;
                double percent = numComplete;
                percent /= files.Count;
                progress.Report(percent * 100);
            }
        }
示例#8
0
        internal async Task Load_File(CancellationToken cancellation, IProgress<Tuple<string, int>> currentTaskAndPercentComplete)
        {
            if (UnsavedChangesPresent && !_window.Confirm_Discard_Changes()) return;

            var dialog = new OpenFileDialog
            {
                DefaultExt = DataFileExt,
                Filter = DataFileFilter
            };
            if (dialog.ShowDialog() ?? false)
            {
                FileList = await FileAccess.LoadInfo(dialog.FileName, cancellation, currentTaskAndPercentComplete);
                if (cancellation.IsCancellationRequested)
                {
                    return;
                }

                UnsavedChangesPresent = false;
                if (FileList.Any())
                {
                    FileIndex = 0;
                    RectangleIndex = 0;
                    RectangleHasFocus = false;
                    await _window.Canvas.LoadImage(this, FileIndex);
                    _window.Canvas.LoadRectangles(this, FileIndex);
                }
            }
        }
        internal static Task<Report> CreateReport(string projectPath, CancellationToken cancellationToken, IProgress<string> progress, ICodeInspectSettings codeInspectSettings)
        {
            TaskCompletionSource<Report> completedTask = new TaskCompletionSource<Report>();
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    completedTask.TrySetCanceled();
                    return completedTask.Task;
                }

                if (projectPath.EndsWith("xml"))
                {
                    completedTask.TrySetResult(CreateReportFromXml(projectPath));
                }
                else
                {
                    return CreateReportFromProject(
                            codeInspectSettings.InspectCodePath,
                            projectPath,
                            Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), DateTime.Now.Ticks + ".xml"),
                            progress: progress);
                }
            }
            catch (Exception ex)
            {
                completedTask.TrySetException(ex);
            }

            return completedTask.Task;
        }
示例#10
0
        private async Task SyncInternal(ServerInfo server, IProgress<double> progress, CancellationToken cancellationToken)
        {
            _logger.Debug("Beginning ServerSync with server {0}, Id {1}", server.Name, server.Id);
            
            if (string.IsNullOrWhiteSpace(server.AccessToken) && string.IsNullOrWhiteSpace(server.ExchangeToken))
            {
                _logger.Info("Skipping sync process for server " + server.Name + ". No server authentication information available.");
                progress.Report(100);
                return;
            }

            // Don't need these here
            var result = await _connectionManager.Connect(server, new ConnectionOptions
            {
                EnableWebSocket = false,
                ReportCapabilities = false,
                UpdateDateLastAccessed = false

            }, cancellationToken).ConfigureAwait(false);

            if (result.State == ConnectionState.SignedIn)
            {
                await SyncInternal(server, result.ApiClient, progress, cancellationToken).ConfigureAwait(false);
                progress.Report(100);
            }
            else
            {
                _logger.Info("Skipping sync process for server " + server.Name + ". ConnectionManager returned a state of {0}", result.State.ToString());
                progress.Report(100);
            }
        }
示例#11
0
 private static async Task DownloadToCacheFileAsync(Uri uri, string fileName, IProgress<int> progress)
 {
     try
     {
         using (var client = new HttpClient())
         {
             var request = new HttpRequestMessage(HttpMethod.Get, uri);
             var response = await client.SendAsync(request);
             response.EnsureSuccessStatusCode();
             long length = response.Content.Headers.ContentLength ?? 0;
             using (var responseStream = await response.Content.ReadAsStreamAsync())
             using (var fileStream = await CreateTempFileStreamAsync(fileName))
             {
                 IProgress<long> absoluteProgress = null;
                 if (progress != null)
                 {
                     absoluteProgress =
                         new Progress<long>(bytesCopied =>
                         {
                             if (length > 0)
                                 progress.Report((int) (100*bytesCopied/length));
                             else
                                 progress.Report(-1);
                         });
                 }
                 await responseStream.CopyToAsync(fileStream, absoluteProgress);
             }
         }
     }
     catch
     {
         await DeleteTempFileAsync(fileName);
         throw;
     }
 }
示例#12
0
        /// <summary>
        /// Runs the specified progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var allSongs = _libraryManager.RootFolder
                .GetRecursiveChildren(i => !i.IsFolder && (i is IHasArtist))
                .Cast<IHasArtist>()
                .ToList();

            var allArtists = _libraryManager.GetArtists(allSongs).ToList();

            var numComplete = 0;
            var numArtists = allArtists.Count;

            foreach (var artistItem in allArtists)
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    await artistItem.RefreshMetadata(cancellationToken).ConfigureAwait(false);
                }
                catch (IOException ex)
                {
                    _logger.ErrorException("Error validating Artist {0}", ex, artistItem.Name);
                }

                // Update progress
                numComplete++;
                double percent = numComplete;
                percent /= numArtists;

                progress.Report(100 * percent);
            }
        }
示例#13
0
 public static IEnumerable<AnnotationRepository> CreateRepositoriesFromFolder(string folderPath, IProgress progress)
 {
     foreach (var path in GetChorusNotesFilePaths(folderPath))
     {
         yield return AnnotationRepository.FromFile("id", path, progress);
     }
 }
示例#14
0
        private DataResult<IList<Employee>> LoadEmployees(CancellationToken token, IProgress<double> progress)
        {
            var employees = XDocument.Load(@"data\sampledata.xml")
                .Descendants("Employee")
                .Select(x => new Employee {
                    Number = int.Parse(x.Descendants("Number").First().Value),
                    Name = x.Descendants("Name").First().Value,
                    Surname = x.Descendants("Surname").First().Value,
                    Salary = double.Parse(x.Descendants("Salary").First().Value)
                })
                .ToList();

            var itemIndex = 0;
            var result = new DataResult<IList<Employee>> {
                Result = new List<Employee>()
            };

            try {
                foreach (var employee in employees) {
                    Thread.CurrentThread.Join(100);
                    result.Result.Add(employee);
                    token.ThrowIfCancellationRequested();
                    progress.Report((++itemIndex*100)/employees.Count);
                }
            }
            catch (OperationCanceledException ex) {
                result.Exception = ex;
            }

            return result;
        }
示例#15
0
 public virtual Task<string> Put(string key, Stream stream, IProgress<ProgressInBytes> progress = null, CancellationToken cancellation = default(CancellationToken))
 {
     var bytes = new byte[stream.Length];
     stream.Read(bytes, 0, (int)stream.Length);
     store.GetOrAdd(key, bytes);
     return Task.FromResult(key);
 }
示例#16
0
        public static void Decompress(Stream inStream, Stream outStream, IProgress<ProgressReport> progress)
        {
            byte[] properties = new byte[5];

            if (inStream.Read(properties, 0, 5) != 5)
                throw (new Exception("input .lzma is too short"));

            SevenZip.Compression.LZMA.Decoder decoder = new SevenZip.Compression.LZMA.Decoder();
            decoder.SetDecoderProperties(properties);

            long outSize = 0;
            for (int i = 0; i < 8; i++)
            {
                int v = inStream.ReadByte();
                if (v < 0)
                    throw (new Exception("Can't Read 1"));
                outSize |= ((long)(byte)v) << (8 * i);
            }

            long compressedSize = inStream.Length - inStream.Position;
            var lzmaProgress = new LzmaProgress(progress, "Decompressing", outSize, MeasureBy.Output);
            lzmaProgress.SetProgress(0, 0);
            decoder.Code(inStream, outStream, compressedSize, outSize, lzmaProgress);
            lzmaProgress.SetProgress(inStream.Length, outSize);
        }
示例#17
0
		internal static void PushHumptyOffTheWall(IProgress progress, bool writeVerbose, string mainFilePathname)
		{
			Guard.AgainstNull(progress, "progress");
			FileWriterService.CheckFilename(mainFilePathname);

			var rootDirectoryName = Path.GetDirectoryName(mainFilePathname);
			// NB: This is strictly an ordered list of method calls.
			// Don't even 'think' of changing any of them.
			CheckForUserCancelRequested(progress);
			DeleteOldFiles(rootDirectoryName);
			CheckForUserCancelRequested(progress);
			WriteVersionFile(mainFilePathname);
			// Outer Dict has the class name for its key and a sorted (by guid) dictionary as its value.
			// The inner dictionary has a caseless guid as the key and the byte array as the value.
			// (Only has current concrete classes.)
			var classData = GenerateBasicClassData();
			var wellUsedElements = new Dictionary<string, XElement>
				{
					{SharedConstants.LangProject, null},
					{SharedConstants.LexDb, null}
				};
			var guidToClassMapping = WriteOrCacheProperties(mainFilePathname, classData, wellUsedElements);
			CheckForUserCancelRequested(progress);
			BaseDomainServices.PushHumptyOffTheWall(progress, writeVerbose, rootDirectoryName, wellUsedElements, classData, guidToClassMapping);

#if DEBUG
			// Enable ONLY for testing a round trip.
			// FLExProjectUnifier.PutHumptyTogetherAgain(progress, writeVerbose, mainFilePathname);
#endif
		}
        /// <summary>
        /// </summary>
        /// <param name="path"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public Task<GenerationResult> Generate(string path, IProgress<ProgressResult> progress)
        {
            return Task.Run(() => {
                var db = Schema.Database;
                var content = Resources.selection_builder;
                var output = PathUtils.FilePath(path, db.PackageName, db.ProviderFolder + Constants.Util);

                content = string.Format(content, db.PackageName, db.ProviderFolder + Constants.Util);

                if (progress != null) {
                    progress.Report(new ProgressResult {
                        Name = this.GetType().Name,
                        Value = 1
                    });
                }

                return new GenerationResult{
                    Content = new List<string>{
                        content
                    },
                    Path = new List<string>{
                        output + "SelectionBuilder.java"
                    }
                };
            });
        }
示例#19
0
        /// <summary>
        /// Returns the task to be executed
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="progress">The progress.</param>
        /// <returns>Task.</returns>
        public Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
        {
            return Task.Run(() =>
            {
                // Delete log files more than n days old
                var minDateModified = DateTime.UtcNow.AddDays(-(ConfigurationManager.CommonConfiguration.LogFileRetentionDays));

                var filesToDelete = new DirectoryInfo(ConfigurationManager.CommonApplicationPaths.LogDirectoryPath).EnumerateFileSystemInfos("*", SearchOption.AllDirectories)
                              .Where(f => f.LastWriteTimeUtc < minDateModified)
                              .ToList();

                var index = 0;

                foreach (var file in filesToDelete)
                {
                    double percent = index;
                    percent /= filesToDelete.Count;

                    progress.Report(100 * percent);

                    cancellationToken.ThrowIfCancellationRequested();

                    File.Delete(file.FullName);

                    index++;
                }

                progress.Report(100);
            });
        }
示例#20
0
 public LzmaProgress(IProgress<ProgressReport> progress, string phase, long totalSize, MeasureBy measureBy)
 {
     this.progress = progress;
     this.totalSize = totalSize;
     this.phase = phase;
     this.measureBy = measureBy;
 }
示例#21
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Import a file containing translations for one or more lists.
		/// </summary>
		/// ------------------------------------------------------------------------------------
		public bool ImportTranslatedLists(string filename, FdoCache cache, IProgress progress)
		{
#if DEBUG
			DateTime dtBegin = DateTime.Now;
#endif
			using (var inputStream = FileUtils.OpenStreamForRead(filename))
			{
				var type = Path.GetExtension(filename).ToLowerInvariant();
				if (type == ".zip")
				{
					using (var zipStream = new ZipInputStream(inputStream))
					{
						var entry = zipStream.GetNextEntry(); // advances it to where we can read the one zipped file.
						using (var reader = new StreamReader(zipStream, Encoding.UTF8))
							ImportTranslatedLists(reader, cache, progress);
					}
				}
				else
				{
					using (var reader = new StreamReader(inputStream, Encoding.UTF8))
						ImportTranslatedLists(reader, cache, progress);
				}
			}
#if DEBUG
			DateTime dtEnd = DateTime.Now;
			TimeSpan span = new TimeSpan(dtEnd.Ticks - dtBegin.Ticks);
			Debug.WriteLine(String.Format("Elapsed time for loading translated list(s) from {0} = {1}",
				filename, span.ToString()));
#endif
			return true;
		}
			public async Task<IEnumerable<Tuple<string, string>>> PlayableStreams(IResourceNetworkLayer networkLayer, IProgress<float> progress, System.Threading.CancellationToken cancelToken)
			{
				string gfyName = GetGfyName(_originalUrl);
				var jsonResult = await networkLayer.Get("http://gfycat.com/cajax/get/" + gfyName, cancelToken, progress, null, false);
				var gfyResult = JsonConvert.DeserializeObject<GfyResult>(jsonResult);
				return new List<Tuple<string, string>> { Tuple.Create(gfyResult.gfyItem.mp4Url, "MP4") };
			}
示例#23
0
        /// <summary>
        /// Runs the specified progress.
        /// </summary>
        /// <param name="progress">The progress.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>Task.</returns>
        public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var names = _itemRepo.GetMusicGenreNames();

            var numComplete = 0;
            var count = names.Count;

            foreach (var name in names)
            {
                try
                {
                    var item = _libraryManager.GetMusicGenre(name);

                    await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    // Don't clutter the log
                    break;
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error refreshing {0}", ex, name);
                }

                numComplete++;
                double percent = numComplete;
                percent /= count;
                percent *= 100;

                progress.Report(percent);
            }

            progress.Report(100);
        }
示例#24
0
		public static bool TryMergeEntries(LexEntry entry1, LexEntry entry2, string[] traitsWithMultiplicity, IProgress progress)
		{
			if (!entry1.LexicalForm.CanBeUnifiedWith(entry2.LexicalForm))
			{
				progress.WriteMessageWithColor("gray","Attempting to merge entries, but could not because their Lexical Forms clash in some writing system.");
				return false;
			}

			if (!SenseMerger.TryMergeProperties(entry1, entry2, traitsWithMultiplicity, "entries for "+entry1.ToString(), progress))
				return false;

			// at this point, we're committed to doing the merge

			entry1.LexicalForm.MergeIn(entry2.LexicalForm);

			var senses = entry2.Senses.ToArray();
			foreach (var sense in senses)
			{
				MergeOrAddSense(entry1, sense, traitsWithMultiplicity, progress);
			}

			if (entry2.ModificationTime > entry1.ModificationTime)
			{
				entry1.ModificationTime = entry2.ModificationTime;
			}

			entry1.IsDirty = true;
			return true;
		}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <param name="progress"></param>
        /// <returns></returns>
        public async Task Execute(CancellationToken cancellationToken, IProgress<double> progress)
        {
            var users = _userManager.Users.Where(u => UserHelper.GetTraktUser(u) != null).ToList();

            // No point going further if we don't have users.
            if (users.Count == 0)
            {
                _logger.Info("No Users returned");
                return;
            }

            // purely for progress reporting
            var percentPerUser = 100 / users.Count;
            double currentProgress = 0;
            var numComplete = 0;

            foreach (var user in users)
            {
                try
                {
                    await SyncTraktDataForUser(user, currentProgress, cancellationToken, progress, percentPerUser).ConfigureAwait(false);

                    numComplete++;
                    currentProgress = percentPerUser * numComplete;
                    progress.Report(currentProgress);
                }
                catch (Exception ex)
                {
                    _logger.ErrorException("Error syncing trakt data for user {0}", ex, user.Name);
                }
            }
        }
        private void ExtractImage(List<WriteableBitmap> tempTiles, BitmapSource image, int spacing, int offset, IProgress<ProgressDialogState> progress = null)
        {
            var sourceImage = BitmapFactory.ConvertToPbgra32Format(image);

            var jump = 16 + spacing;
            var totalTiles = ((image.PixelWidth - offset) / jump) * ((image.PixelHeight - offset) / jump);
            var currentTile = 0;

            for (var y = offset; y < image.PixelHeight; y += jump)
            {
                for (var x = offset; x < image.PixelWidth; x += jump)
                {
                    var tileImage = new WriteableBitmap(16, 16, 96, 96, PixelFormats.Pbgra32, null);
                    tileImage.Blit(new System.Windows.Rect(0, 0, 16, 16), sourceImage, new System.Windows.Rect(x, y, 16, 16));
                    tempTiles.Add(tileImage);

                    currentTile++;
                    if (progress != null)
                    {
                        progress.Report(new ProgressDialogState() {
                            ProgressPercentage = currentTile * 100 / totalTiles,
                            Description = string.Format("Extracting {0} / {1}", currentTile, totalTiles)
                        });
                    }
                }
            }
        }
        private void RunInternal(IProgress<double> progress, CancellationToken cancellationToken)
        {
            var allItems = _libraryManager.RootFolder
                .RecursiveChildren
                .ToList();

            var musicAlbums = allItems
                .OfType<MusicAlbum>()
                .ToList();

            AttachMovieSoundtracks(allItems, musicAlbums, cancellationToken);

            progress.Report(25);

            AttachTvSoundtracks(allItems, musicAlbums, cancellationToken);

            progress.Report(50);

            AttachGameSoundtracks(allItems, musicAlbums, cancellationToken);

            progress.Report(75);

            AttachAlbumLinks(allItems, musicAlbums, cancellationToken);

            progress.Report(100);
        }
示例#28
0
 public override IEnumerable<ISystemEntry> GetChildren(CancellationToken token, IProgress<double> progress)
 {
     return ProcessUtility.GetChildProcessIds(this.ProcessId)
         .WithCancellation(token)
         .Select(id => Process.GetProcessById(id))
         .Select(proc => new ProcessSystemEntry(this, proc.Id.ToString(), proc));
 }
 public Task DoAsync(IProgress<int> Progress)
 {
     Progress.Report(1);
     Thread.Sleep(800);
     Progress.Report(95);
     return Task.FromResult<object>(null);
 }
示例#30
0
 public async Task ExportTexturesAsync(string textureDir, IProgress<string> progress = null, CancellationToken cancellation = default(CancellationToken)) {
     foreach (var texture in Textures.Values) {
         if (cancellation.IsCancellationRequested) return;
         progress?.Report(texture.Name);
         await FileUtils.WriteAllBytesAsync(Path.Combine(textureDir, texture.Name), TexturesData[texture.Name], cancellation);
     }
 }
示例#31
0
        public static Synchronizer FromProjectConfiguration(ProjectFolderConfiguration project, IProgress progress)
        {
            var hg = HgRepository.CreateOrUseExisting(project.FolderPath, progress);

            return(new Synchronizer(hg.PathToRepo, project, progress));
        }
        // Run the tournament async
        public Task RunAsync(int rounds, CrossTable crossTable, CancellationToken cancellationToken, IProgress <RunProgress> progress)
        {
            return(Task.Run(() =>
            {
                int completedMatches = 0;
                int num = crossTable.Players.Count;
                crossTable.Scores.Clear();
                int numGames = num * (num - 1) / 2;
                table = crossTable;
                completedMatches = 0;
                var roundMatchups = crossTable.NextRoundSchedule();

                while (roundMatchups != null)
                {
                    Parallel.For(0, roundMatchups.Count, n => {
                        var match = roundMatchups[n];
                        cancellationToken.ThrowIfCancellationRequested();
                        var player1 = match.Item1;
                        var player2 = match.Item2;
                        if (player1 != null && player2 != null)
                        { // check for bye
                            var headTohead = PlayMatch(rounds, player1, player2);
                            Interlocked.Increment(ref completedMatches);
                            double completedRatio = completedMatches;
                            completedRatio /= numGames;
                            progress.Report(new RunProgress {
                                RatioCompleted = completedRatio, HeadToHeadScore = headTohead
                            });
                        }
                    });
                    roundMatchups = crossTable.NextRoundSchedule();
                }
            }
                            ));
        }
示例#33
0
        private async Task <Tuple <ulong, bool> > GetUrlsAsync(IProgress <DownloadProgress> progress, CancellationToken ct, PauseToken pt)
        {
            var semaphoreSlim        = new SemaphoreSlim(shellService.Settings.ParallelScans);
            var trackedTasks         = new List <Task>();
            var numberOfPostsCrawled = 0;
            var apiLimitHit          = false;
            var completeGrab         = true;

            ulong lastId = GetLastPostId();

            await UpdateTotalPostCount();

            int totalPosts = blog.Posts;

            ulong highestId = await GetHighestPostId();

            foreach (int pageNumber in GetPageNumbers())
            {
                await semaphoreSlim.WaitAsync();

                if (!completeGrab)
                {
                    break;
                }

                if (ct.IsCancellationRequested)
                {
                    break;
                }
                if (pt.IsPaused)
                {
                    pt.WaitWhilePausedWithResponseAsyc().Wait();
                }

                trackedTasks.Add(new Func <Task>(async() =>
                {
                    try
                    {
                        XDocument document = await GetApiPageAsync(pageNumber);

                        completeGrab = CheckPostAge(document, lastId);

                        var tags = new List <string>();
                        if (!string.IsNullOrWhiteSpace(blog.Tags))
                        {
                            tags = blog.Tags.Split(',').Select(x => x.Trim()).ToList();
                        }

                        AddUrlsToDownloadList(document, tags);
                    }
                    catch (WebException webException)
                    {
                        if (webException.Message.Contains("429"))
                        {
                            // TODO: add retry logic?
                            apiLimitHit = true;
                            Logger.Error("TumblrDownloader:GetUrls:WebException {0}", webException);
                            shellService.ShowError(webException, Resources.LimitExceeded, blog.Name);
                        }
                    }
                    finally
                    {
                        semaphoreSlim.Release();
                    }

                    numberOfPostsCrawled += blog.PageSize;
                    UpdateProgressQueueInformation(progress, Resources.ProgressGetUrlLong, numberOfPostsCrawled, totalPosts);
                })());
            }
            await Task.WhenAll(trackedTasks);

            producerConsumerCollection.CompleteAdding();

            if (!ct.IsCancellationRequested && completeGrab)
            {
                UpdateBlogStats();
            }

            return(new Tuple <ulong, bool>(highestId, apiLimitHit));
        }
示例#34
0
        public bool Write(BinaryReader r, BinaryWriter w, TagData tag, IProgress <float> writeProgress = null)
        {
            bool result = true;

            // Constraint-check on non-supported values
            if (FieldCodeFixedLength > 0)
            {
                if (tag.Pictures != null)
                {
                    foreach (PictureInfo picInfo in tag.Pictures)
                    {
                        if (PictureInfo.PIC_TYPE.Unsupported.Equals(picInfo.PicType) && (picInfo.TagType.Equals(getImplementedTagType())))
                        {
                            if ((-1 == picInfo.NativePicCode) && (Utils.ProtectValue(picInfo.NativePicCodeStr).Length != FieldCodeFixedLength))
                            {
                                throw new NotSupportedException("Field code fixed length is " + FieldCodeFixedLength + "; detected field '" + Utils.ProtectValue(picInfo.NativePicCodeStr) + "' is " + Utils.ProtectValue(picInfo.NativePicCodeStr).Length + " characters long and cannot be written");
                            }
                        }
                    }
                }
                foreach (MetaFieldInfo fieldInfo in tag.AdditionalFields)
                {
                    if (fieldInfo.TagType.Equals(getImplementedTagType()) || MetaDataIOFactory.TAG_ANY == fieldInfo.TagType)
                    {
                        string fieldCode = Utils.ProtectValue(fieldInfo.NativeFieldCode);
                        if (fieldCode.Length != FieldCodeFixedLength && !fieldCode.Contains("----")) // "----" = exception for MP4 extended fields (e.g. ----:com.apple.iTunes:CONDUCTOR)
                        {
                            throw new NotSupportedException("Field code fixed length is " + FieldCodeFixedLength + "; detected field '" + fieldCode + "' is " + fieldCode.Length + " characters long and cannot be written");
                        }
                    }
                }
            }

            structureHelper.Clear();
            tagData.Pictures.Clear();

            // Read all the fields in the existing tag (including unsupported fields)
            ReadTagParams readTagParams = new ReadTagParams(true, true);

            readTagParams.PrepareForWriting = true;

            if (embedder != null && embedder.HasEmbeddedID3v2 > 0)
            {
                readTagParams.offset = embedder.HasEmbeddedID3v2;
            }

            this.read(r, readTagParams);

            if (embedder != null && getImplementedTagType() == MetaDataIOFactory.TAG_ID3V2)
            {
                structureHelper.Clear();
                structureHelper.AddZone(embedder.Id3v2Zone);
            }

            // Give engine something to work with if the tag is really empty
            if (!tagExists && 0 == Zones.Count)
            {
                structureHelper.AddZone(0, 0);
            }

            TagData dataToWrite;

            dataToWrite = tagData;
            dataToWrite.IntegrateValues(tag); // Merge existing information + new tag information
            dataToWrite.Cleanup();

            FileSurgeon surgeon = new FileSurgeon(structureHelper, embedder, getImplementedTagType(), getDefaultTagOffset(), writeProgress);

            result = surgeon.RewriteZones(w, new FileSurgeon.WriteDelegate(writeAdapter), Zones, dataToWrite, tagExists);

            // Update tag information without calling Read

            /* TODO - this implementation is too risky :
             *   - if one of the writing operations fails, data is updated as if everything went right
             *   - any picture slot with a markForDeletion flag is recorded as-is in the tag
             */
            tagData = dataToWrite;

            return(result);
        }
示例#35
0
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            LogVersionInformation();
            await base.InitializeAsync(cancellationToken, progress);

            await InitializeLoggingAsync();
            await GetServiceAsync(typeof(IUsageTracker));

            // Avoid delays when there is ongoing UI activity.
            // See: https://github.com/github/VisualStudio/issues/1537
            await JoinableTaskFactory.RunAsync(VsTaskRunContext.UIThreadNormalPriority, InitializeMenus);
        }
示例#36
0
 public TumblrSearchCrawler(IShellService shellService, CancellationToken ct, PauseToken pt, IProgress <DownloadProgress> progress,
                            ICrawlerService crawlerService, IWebRequestFactory webRequestFactory, ISharedCookieService cookieService,
                            IDownloader downloader, IPostQueue <TumblrPost> postQueue, IBlog blog)
     : base(shellService, ct, progress, webRequestFactory, cookieService, postQueue, blog)
 {
     this.downloader = downloader;
     this.pt         = pt;
 }
        /// <summary>
        /// The entry point for the extension. This is executed when the tool window is activated.
        /// </summary>
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            await NewsWindowCommand.InitializeAsync(this);
        }
示例#38
0
 protected abstract Task <IReadOnlyCollection <TChild> > LoadChildrenAsync(IProgress <TChild> progress);
        public MainAppContext(IAppSetting appSetting, IDnsManager dnsManager, IFileUpdater fileUpdater)
        {
            // Get config settings.
            _appSetting = appSetting;

            // Populate fields.
            _dnsManager  = dnsManager;
            _fileUpdater = fileUpdater;

            // Subscribed to the application's exit event.
            Application.ApplicationExit += OnApplicationExit;

            // Make sure the 'Data' folder exists.
            if (!Directory.Exists(_appSetting.StorageInfo.Path))
            {
                Directory.CreateDirectory(_appSetting.StorageInfo.Path);
            }

            // Construct the notification's context menu.
            var contextMenu = new ContextMenuStrip();

            // Create menus for DNS entries.
            var tempUpdateDNSMenus = new List <ToolStripItem>();

            foreach (Source source in _appSetting.Sources)
            {
                tempUpdateDNSMenus.Add(MenuBuilder.CreateMenu(source.Name).HookClickEvent(OnSetDNSMenuClicked));
            }
            _updateDNSMenus = new ToolStripMenuItem[tempUpdateDNSMenus.Count];
            tempUpdateDNSMenus.CopyTo(_updateDNSMenus);

            // Other essential menus.
            contextMenu.Items.AddRange(new ToolStripItem[]
            {
                MenuBuilder.CreateMenu($"DNS Resolver v{Application.ProductVersion}"),
                MenuBuilder.CreateMenu("Querying host file data..."),
                MenuBuilder.CreateMenu("DNS").AddManyChildMenu(_updateDNSMenus),
                MenuBuilder.CreateSeparator(),
                MenuBuilder.CreateMenu("Open Host File...").HookClickEvent(OnOpenHostFileMenuClicked),
                MenuBuilder.CreateSeparator(),
                MenuBuilder.CreateMenu("Reset DNS").HookClickEvent(OnDNSResetMenuClicked),
                MenuBuilder.CreateMenu("Clear Host File").HookClickEvent(OnHostFileResetMenuClicked),
                MenuBuilder.CreateSeparator(),
                MenuBuilder.CreateMenu("Exit").HookClickEvent(OnExitMenuClicked),
            });
            contextMenu.Renderer = new CustomToolStripMenuRenderer();

            // Make sure that the menu checked condition is refreshed everytime it is opening.
            contextMenu.Opening += OnNotifyIconContextMenuOpening;

            // Finally, the notification icon itself.
            _notifyIcon = new NotifyIcon
            {
                Icon             = Resources.AppIcon,
                Text             = Application.ProductName,
                Visible          = true,
                ContextMenuStrip = contextMenu,
                BalloonTipIcon   = ToolTipIcon.Info,
                BalloonTipTitle  = Application.ProductName
            };

            // Update the notify icon's text.
            UpdateNotifyIconText();

            // Create menus for Host file data entries.
            _hostFileProgress = new Progress <ToolStripMenuItem[]>(HostFileMenusProgressHandler);

            Task.Run(() => ConstructHostFileContextMenu());
        }
示例#40
0
        public override async Task ExecuteAsync(ReportRequest request,
                                                CancellationToken token,
                                                IProgress <OperationStatus> progress = null)
        {
            #region Reporting initialization
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            request = await StartRequestAsync(request);

            var criterion
                = await _serviceFacade.ReportCriterionRepository.GetByIdAsync(request.ReportCriteriaId)
                  ?? throw new GraException($"Report criteria {request.ReportCriteriaId} for report request id {request.Id} could not be found.");

            if (criterion.SiteId == null)
            {
                throw new ArgumentNullException(nameof(criterion.SiteId));
            }

            string title = "";

            if (criterion.SystemId.HasValue)
            {
                title = (await _systemRepository.GetByIdAsync(criterion.SystemId.Value)).Name;
            }

            var report = new StoredReport
            {
                Title = title,
                AsOf  = _serviceFacade.DateTimeProvider.Now
            };
            var reportData = new List <object[]>();
            #endregion Reporting initialization

            #region Collect data
            UpdateProgress(progress, 1, "Starting report...", request.Name);

            // header row
            report.HeaderRow = new object[] {
                criterion.SystemId.HasValue ? "System Name" : "Branch Name"
            };

            var programs = await _programRepository.GetAllAsync(criterion.SiteId.Value);

            foreach (var program in programs)
            {
                report.HeaderRow = report.HeaderRow.Append(program.Name);
            }

            report.HeaderRow = report.HeaderRow.Append("Total");

            int count = 0;

            var users = await _userRepository.GetUsersByCriterionAsync(criterion);

            if (criterion.SystemId.HasValue)
            {
                users = users.Where(_ => _.SystemId == criterion.SystemId);
                var branches = await _branchRepository.GetBySystemAsync(criterion.SystemId.Value);

                foreach (var branch in branches)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    UpdateProgress(progress,
                                   ++count * 100 / branches.Count(),
                                   $"Processing: {branch.SystemName} - {branch.Name}",
                                   request.Name);

                    var branchUsers          = users.Where(_ => _.BranchId == branch.Id);
                    IEnumerable <object> row = new object[]
                    {
                        branch.Name
                    };
                    foreach (var program in programs)
                    {
                        row = row.Append(branchUsers.Where(_ => _.ProgramId == program.Id).Count());
                    }
                    row = row.Append(branchUsers.Count());
                    reportData.Add(row.ToArray());
                }
            }
            else
            {
                var systems = await _systemRepository.GetAllAsync(criterion.SiteId.Value);

                foreach (var system in systems)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }

                    UpdateProgress(progress,
                                   ++count * 100 / systems.Count(),
                                   $"Processing: {system.Name}",
                                   request.Name);

                    var systemUsers          = users.Where(_ => _.SystemId == system.Id);
                    IEnumerable <object> row = new object[]
                    {
                        system.Name
                    };
                    foreach (var program in programs)
                    {
                        row = row.Append(systemUsers.Where(_ => _.ProgramId == program.Id).Count());
                    }
                    row = row.Append(systemUsers.Count());
                    reportData.Add(row.ToArray());
                }
            }

            report.Data = reportData.ToArray();

            IEnumerable <object> footerRow = new object[]
            {
                "Total"
            };
            foreach (var program in programs)
            {
                footerRow = footerRow.Append(users.Where(_ => _.ProgramId == program.Id).Count());
            }
            footerRow = footerRow.Append(users.Count());

            report.FooterRow = footerRow.ToArray();

            #endregion Collect data

            #region Finish up reporting
            _logger.LogInformation($"Report {GetType().Name} with criterion {criterion.Id} ran in {StopTimer()}");

            request.Success = !token.IsCancellationRequested;

            if (request.Success == true)
            {
                ReportSet.Reports.Add(report);
                request.Finished   = _serviceFacade.DateTimeProvider.Now;
                request.ResultJson = Newtonsoft.Json.JsonConvert.SerializeObject(ReportSet);
            }
            await _serviceFacade.ReportRequestRepository.UpdateSaveNoAuditAsync(request);

            #endregion Finish up reporting
        }
示例#41
0
 protected override Task ValidateChildrenInternal(IProgress <double> progress, System.Threading.CancellationToken cancellationToken, bool recursive, bool refreshChildMetadata, Providers.MetadataRefreshOptions refreshOptions, Providers.IDirectoryService directoryService)
 {
     return(Task.FromResult(true));
 }
示例#42
0
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await base.InitializeAsync(cancellationToken, progress);

            var componentModel = await GetServiceAsync(typeof(SComponentModel)) as IComponentModel;

            _globalRunSettings = componentModel.GetService <IGlobalRunSettingsInternal>();

            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            _generalOptions         = (GeneralOptionsDialogPage)GetDialogPage(typeof(GeneralOptionsDialogPage));
            _parallelizationOptions = (ParallelizationOptionsDialogPage)GetDialogPage(typeof(ParallelizationOptionsDialogPage));
            _googleTestOptions      = (GoogleTestOptionsDialogPage)GetDialogPage(typeof(GoogleTestOptionsDialogPage));

            _globalRunSettings.RunSettings = GetRunSettingsFromOptionPages();

            _generalOptions.PropertyChanged         += OptionsChanged;
            _parallelizationOptions.PropertyChanged += OptionsChanged;
            _googleTestOptions.PropertyChanged      += OptionsChanged;

            SwitchCatchExceptionsOptionCommand.Initialize(this);
            SwitchBreakOnFailureOptionCommand.Initialize(this);
            SwitchParallelExecutionOptionCommand.Initialize(this);
            SwitchPrintTestOutputOptionCommand.Initialize(this);

            DisplayReleaseNotesIfNecessary();

            var logger           = new ActivityLogLogger(this, () => _generalOptions.DebugMode);
            var debuggerAttacher = new VsDebuggerAttacher(this);

            _debuggerAttacherServiceHost = new DebuggerAttacherServiceHost(_debuggingNamedPipeId, debuggerAttacher, logger);
            try
            {
                _debuggerAttacherServiceHost.Open();
            }
            catch (CommunicationException)
            {
                _debuggerAttacherServiceHost.Abort();
                _debuggerAttacherServiceHost = null;
            }
        }
示例#43
0
        static IEnumerator AsObservableCore <T>(T asyncOperation, IObserver <T> observer, IProgress <float> reportProgress, CancellationToken cancel)
            where T : AsyncOperation
        {
            if (reportProgress != null)
            {
                while (!asyncOperation.isDone && !cancel.IsCancellationRequested)
                {
                    try
                    {
                        reportProgress.Report(asyncOperation.progress);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        yield break;
                    }
                    yield return(null);
                }
            }
            else
            {
                if (!asyncOperation.isDone)
                {
                    yield return(asyncOperation);
                }
            }

            if (cancel.IsCancellationRequested)
            {
                yield break;
            }

            if (reportProgress != null)
            {
                try
                {
                    reportProgress.Report(asyncOperation.progress);
                }
                catch (Exception ex)
                {
                    observer.OnError(ex);
                    yield break;
                }
            }

            observer.OnNext(asyncOperation);
            observer.OnCompleted();
        }
示例#44
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param>
        /// <param name="progress">A provider for progress updates.</param>
        /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns>
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            // When initialized asynchronously, the current thread may be a background thread at this point.
            // Do any initialization that requires the UI thread after switching to the UI thread.
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            await MainCommand.InitializeAsync(this);
        }
示例#45
0
 public IErrorsInfo UpdateEntities(string EntityName, object UploadData, IProgress <PassedArgs> progress)
 {
     return(GetDataSourceObject(EntityName).UpdateEntities(EntityName, UploadData, progress));
 }
示例#46
0
文件: YBXF3.cs 项目: micvb123/Yabber
        public static void Unpack(this BXF3Reader bxf, string bhdName, string bdtName, string targetDir, IProgress <float> progress)
        {
            Directory.CreateDirectory(targetDir);
            var xws = new XmlWriterSettings();

            xws.Indent = true;
            var xw = XmlWriter.Create($"{targetDir}\\_yabber-bxf3.xml", xws);

            xw.WriteStartElement("bxf3");

            xw.WriteElementString("bhd_filename", bhdName);
            xw.WriteElementString("bdt_filename", bdtName);
            xw.WriteElementString("version", bxf.Version);
            xw.WriteElementString("format", bxf.Format.ToString());
            xw.WriteElementString("bigendian", bxf.BigEndian.ToString());
            xw.WriteElementString("bitbigendian", bxf.BitBigEndian.ToString());

            YBinder.WriteBinderFiles(bxf, xw, targetDir, progress);
            xw.WriteEndElement();
            xw.Close();
        }
示例#47
0
 /// <summary>
 /// If you needs return value, use AsAsyncOperationObservable instead.
 /// </summary>
 public static IObservable <AsyncOperation> AsObservable(this AsyncOperation asyncOperation, IProgress <float> progress = null)
 {
     return(ObservableUnity.FromCoroutine <AsyncOperation>((observer, cancellation) => AsObservableCore(asyncOperation, observer, progress, cancellation)));
 }
示例#48
0
 // T: where T : AsyncOperation is ambigious with IObservable<T>.AsObservable
 public static IObservable <T> AsAsyncOperationObservable <T>(this T asyncOperation, IProgress <float> progress = null)
     where T : AsyncOperation
 {
     return(ObservableUnity.FromCoroutine <T>((observer, cancellation) => AsObservableCore(asyncOperation, observer, progress, cancellation)));
 }
示例#49
0
        /// <summary>
        /// Transfers a file from the source FTP Server to the destination FTP Server via the FXP protocol asynchronously.
        /// </summary>
        private async Task <bool> TransferFileFXPInternalAsync(string sourcePath, FtpClient remoteClient, string remotePath, bool createRemoteDir, FtpRemoteExists existsMode,
                                                               IProgress <FtpProgress> progress, CancellationToken token, FtpProgress metaProgress)
        {
            FtpReply reply;
            long     offset     = 0;
            bool     fileExists = false;
            long     fileSize   = 0;

            var ftpFxpSession = await OpenPassiveFXPConnectionAsync(remoteClient, progress != null, token);

            if (ftpFxpSession != null)
            {
                try {
                    ftpFxpSession.SourceServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;
                    ftpFxpSession.TargetServer.ReadTimeout = (int)TimeSpan.FromMinutes(30.0).TotalMilliseconds;


                    // check if the file exists, and skip, overwrite or append
                    if (existsMode == FtpRemoteExists.ResumeNoCheck)
                    {
                        offset = await remoteClient.GetFileSizeAsync(remotePath, -1, token);

                        if (offset == -1)
                        {
                            offset = 0;                             // start from the beginning
                        }
                    }
                    else
                    {
                        fileExists = await remoteClient.FileExistsAsync(remotePath, token);

                        switch (existsMode)
                        {
                        case FtpRemoteExists.Skip:

                            if (fileExists)
                            {
                                LogStatus(FtpTraceLevel.Info, "Skipping file because Skip is enabled and file already exists (Source: " + sourcePath + ", Dest: " + remotePath + ")");

                                //Fix #413 - progress callback isn't called if the file has already been uploaded to the server
                                //send progress reports
                                if (progress != null)
                                {
                                    progress.Report(new FtpProgress(100.0, 0, 0, TimeSpan.FromSeconds(0), sourcePath, remotePath, metaProgress));
                                }

                                return(true);
                            }

                            break;

                        case FtpRemoteExists.Overwrite:

                            if (fileExists)
                            {
                                await remoteClient.DeleteFileAsync(remotePath, token);
                            }

                            break;

                        case FtpRemoteExists.Resume:

                            if (fileExists)
                            {
                                offset = await remoteClient.GetFileSizeAsync(remotePath, 0, token);
                            }

                            break;
                        }
                    }

                    fileSize = await GetFileSizeAsync(sourcePath, -1, token);

                    // ensure the remote dir exists .. only if the file does not already exist!
                    if (createRemoteDir && !fileExists)
                    {
                        var dirname = remotePath.GetFtpDirectoryName();
                        if (!await remoteClient.DirectoryExistsAsync(dirname, token))
                        {
                            await remoteClient.CreateDirectoryAsync(dirname, token);
                        }
                    }

                    if (offset == 0 && existsMode != FtpRemoteExists.ResumeNoCheck)
                    {
                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"RETR {sourcePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to store the file
                        if (!(reply = await ftpFxpSession.TargetServer.ExecuteAsync($"STOR {remotePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }
                    else
                    {
                        //tell source server to restart / resume
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"REST {offset}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        // send command to tell the source server to 'send' the file to the destination server
                        if (!(reply = await ftpFxpSession.SourceServer.ExecuteAsync($"RETR {sourcePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }

                        //Instruct destination server to append the file
                        if (!(reply = await ftpFxpSession.TargetServer.ExecuteAsync($"APPE {remotePath}", token)).Success)
                        {
                            throw new FtpCommandException(reply);
                        }
                    }

                    var  transferStarted = DateTime.Now;
                    long lastSize        = 0;


                    var sourceFXPTransferReply      = ftpFxpSession.SourceServer.GetReplyAsync(token);
                    var destinationFXPTransferReply = ftpFxpSession.TargetServer.GetReplyAsync(token);

                    // while the transfer is not complete
                    while (!sourceFXPTransferReply.IsCompleted || !destinationFXPTransferReply.IsCompleted)
                    {
                        // send progress reports every 1 second
                        if (ftpFxpSession.ProgressServer != null)
                        {
                            // send progress reports
                            if (progress != null && fileSize != -1)
                            {
                                offset = await ftpFxpSession.ProgressServer.GetFileSizeAsync(remotePath, -1, token);

                                if (offset != -1 && lastSize <= offset)
                                {
                                    long bytesProcessed = offset - lastSize;
                                    lastSize = offset;
                                    ReportProgress(progress, fileSize, offset, bytesProcessed, DateTime.Now - transferStarted, sourcePath, remotePath, metaProgress);
                                }
                            }
                        }

                        await Task.Delay(FXPProgressInterval, token);
                    }

                    FtpTrace.WriteLine(FtpTraceLevel.Info, $"FXP transfer of file {sourcePath} has completed");

                    await NoopAsync(token);

                    await remoteClient.NoopAsync(token);

                    ftpFxpSession.Dispose();

                    return(true);
                }

                // Fix: catch all exceptions and dispose off the FTP clients if one occurs
                catch (Exception ex) {
                    ftpFxpSession.Dispose();
                    throw ex;
                }
            }
            else
            {
                FtpTrace.WriteLine(FtpTraceLevel.Error, "Failed to open FXP passive Connection");
                return(false);
            }
        }
示例#50
0
 public IErrorsInfo UpdateEntities(string EntityName, object UploadData, IProgress <PassedArgs> progress)
 {
     throw new NotImplementedException();
 }
示例#51
0
        /// <summary>
        /// Validate if all required output files exist
        /// </summary>
        /// <param name="basePath">Base filename and path to use for checking</param>
        /// <param name="system">KnownSystem type representing the media</param>
        /// <param name="type">MediaType type representing the media</param>
        /// <param name="progress">Optional result progress callback</param>
        /// <returns></returns>
        public override bool CheckAllOutputFilesExist(string basePath, KnownSystem?system, MediaType?type, IProgress <Result> progress = null)
        {
            string missingFiles = string.Empty;

            switch (type)
            {
            case MediaType.UMD:
                if (!File.Exists($"{basePath}_disc.txt"))
                {
                    missingFiles += $";{basePath}_disc.txt";
                }
                if (!File.Exists($"{basePath}_mainError.txt"))
                {
                    missingFiles += $";{basePath}_mainError.txt";
                }
                if (!File.Exists($"{basePath}_mainInfo.txt"))
                {
                    missingFiles += $";{basePath}_mainInfo.txt";
                }
                if (!File.Exists($"{basePath}_volDesc.txt"))
                {
                    missingFiles += $";{basePath}_volDesc.txt";
                }

                break;

            default:
                // Non-dumping commands will usually produce no output, so this is irrelevant
                return(true);
            }

            // Use the missing files list as an indicator
            if (string.IsNullOrEmpty(missingFiles))
            {
                return(true);
            }
            else
            {
                progress?.Report(Result.Failure($"The following files were missing: {missingFiles.TrimStart(';')}"));
                return(false);
            }
        }
示例#52
0
        /// <summary>
        /// Transfer the specified file from the source FTP Server to the destination FTP Server asynchronously using the FXP protocol.
        /// High-level API that takes care of various edge cases internally.
        /// </summary>
        /// <param name="sourcePath">The full or relative path to the file on the source FTP Server</param>
        /// <param name="remoteClient">Valid FTP connection to the destination FTP Server</param>
        /// <param name="remotePath">The full or relative path to destination file on the remote FTP Server</param>
        /// <param name="createRemoteDir">Indicates if the folder should be created on the remote FTP Server</param>
        /// <param name="existsMode">If the file exists on disk, should we skip it, resume the download or restart the download?</param>
        /// <param name="verifyOptions">Sets if checksum verification is required for a successful download and what to do if it fails verification (See Remarks)</param>
        /// <param name="progress">Provide a callback to track download progress.</param>
        /// <param name="token">The token that can be used to cancel the entire process</param>
        /// Returns a FtpStatus indicating if the file was transfered.
        /// <remarks>
        /// If verification is enabled (All options other than <see cref="FtpVerify.None"/>) the hash will be checked against the server.  If the server does not support
        /// any hash algorithm, then verification is ignored.  If only <see cref="FtpVerify.OnlyChecksum"/> is set then the return of this method depends on both a successful
        /// upload &amp; verification.  Additionally, if any verify option is set and a retry is attempted then overwrite will automatically be set to true for subsequent attempts.
        /// </remarks>
        public async Task <FtpStatus> TransferFileAsync(string sourcePath, FtpClient remoteClient, string remotePath,
                                                        bool createRemoteDir = false, FtpRemoteExists existsMode = FtpRemoteExists.Resume, FtpVerify verifyOptions = FtpVerify.None, IProgress <FtpProgress> progress = null, FtpProgress metaProgress = null, CancellationToken token = default(CancellationToken))
        {
            sourcePath = sourcePath.GetFtpPath();
            remotePath = remotePath.GetFtpPath();

            LogFunc(nameof(TransferFileAsync), new object[] { sourcePath, remoteClient, remotePath, FXPDataType, createRemoteDir, existsMode, verifyOptions });

            // verify input params
            VerifyTransferFileParams(sourcePath, remoteClient, remotePath, existsMode);

            // ensure source file exists
            if (!await FileExistsAsync(sourcePath, token))
            {
                throw new FtpException("Source File " + sourcePath + " cannot be found or does not exists!");
            }

            bool fxpSuccess;
            var  verified     = true;
            var  attemptsLeft = verifyOptions.HasFlag(FtpVerify.Retry) ? m_retryAttempts : 1;

            do
            {
                fxpSuccess = await TransferFileFXPInternalAsync(sourcePath, remoteClient, remotePath, createRemoteDir, existsMode, progress, token, metaProgress is null?new FtpProgress(1, 0) : metaProgress);

                attemptsLeft--;

                // if verification is needed
                if (fxpSuccess && verifyOptions != FtpVerify.None)
                {
                    verified = await VerifyFXPTransferAsync(sourcePath, remoteClient, remotePath, token);

                    LogStatus(FtpTraceLevel.Info, "File Verification: " + (verified ? "PASS" : "FAIL"));
                    if (!verified && attemptsLeft > 0)
                    {
                        LogStatus(FtpTraceLevel.Verbose, "Retrying due to failed verification." + (existsMode == FtpRemoteExists.Resume ? "  Overwrite will occur." : "") + "  " + attemptsLeft + " attempts remaining");
                        // Force overwrite if a retry is required
                        existsMode = FtpRemoteExists.Overwrite;
                    }
                }
            } while (!verified && attemptsLeft > 0);

            if (fxpSuccess && !verified && verifyOptions.HasFlag(FtpVerify.Delete))
            {
                await remoteClient.DeleteFileAsync(remotePath, token);
            }

            if (fxpSuccess && !verified && verifyOptions.HasFlag(FtpVerify.Throw))
            {
                throw new FtpException("Destination file checksum value does not match source file");
            }

            return(fxpSuccess && verified ? FtpStatus.Success : FtpStatus.Failed);
        }
示例#53
0
        public async Task <ChatLog> GetChatLogAsync(AuthToken token, string channelId,
                                                    DateTimeOffset?after = null, DateTimeOffset?before = null, IProgress <double>?progress = null)
        {
            // Get channel
            var channel = await GetChannelAsync(token, channelId);

            // Get the chat log
            return(await GetChatLogAsync(token, channel, after, before, progress));
        }
示例#54
0
        /// <summary>
        /// CALCULO DE FATORIAL ASSINCRONO COM SUPORTE A CANCELAMENTO E BARRA DE PROGRESSO
        /// </summary>
        /// <param name="valorCliente">VALOR QUE USUARIO DIGITOU</param>
        /// <param name="cancellationToken">VERIFICA SE O CLIENTE CLICOU NO BOTÃO DE CANCELAR</param>
        /// <param name="progresso">USAMOS A INTERFACE DE PROGRESS PARA ENVIAR O REPORT DE ATUALIZAÇÃO</param>
        /// <returns>LISTA DE NUMEROS FATORIAIS</returns>
        internal static async Task <string> RetornaNumerosContadorAsyncCancelProgress(int valorCliente, CancellationToken cancellationToken, IProgress <int> progresso)
        {
            return(await Task.Run(() =>
            {
                var retorno = new BigInteger(1);
                for (int i = 1; i < valorCliente; i++)
                {
                    //CASO O CLIENTE SOLICITE O CANCELAMENTO EU
                    //APRESENTO UMA EXCEPTION
                    cancellationToken.ThrowIfCancellationRequested();

                    retorno *= i;

                    //TAMBEM DEVO NOTIFICAR O CLIENTE CASO TENHA ALGUM PROGRESSO
                    //A CADA 10 NOTIFICO A APLICAÇÃO QUE EXISTE UMA ATUALIZAÇÃO
                    if (i % 10 == 0)
                    {
                        progresso.Report(i);
                    }
                }

                return Convert.ToString(retorno);
            }));
        }
示例#55
0
 public ICacheChip <T> CreateCacheChip(string hashKey, DateTime?expTime = null, IProgress <int> progress = null)
 {
     if (this._cacheIndexs.ContainsKey(hashKey))
     {
         return(_cacheIndexs[hashKey]);
     }
     else
     {
         ICacheChip <T> instance = new ServiceCacheChip <T>(this._cacheIndexs, hashKey, this._cacheFolder, expTime);
         this._cacheIndexs.Add(hashKey, instance);
         return(instance);
     }
 }
示例#56
0
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            var menuCommandService = (IMenuCommandService) await GetServiceAsync(typeof(IMenuCommandService));

            // ReSharper disable ObjectCreationAsStatement
            new SetUtf8WithBomCommand(menuCommandService);

            var statusBar = new StatusBarProvider(Application.Current.MainWindow);

            DocumentInfoControl.AddTo(statusBar);
            Debug.WriteLine("* Init Harurow.One *");
        }
示例#57
0
        public async ValueTask <IncomingResponseFrame> SendRequestAsync(
            OutgoingRequestFrame outgoingRequest,
            bool oneway,
            bool synchronous,
            IInvocationObserver?observer,
            IProgress <bool> progress,
            CancellationToken cancel)
        {
            cancel.ThrowIfCancellationRequested();

            IChildInvocationObserver?childObserver = null;
            int requestId = 0;

            // The CollocatedRequestHandler is an internal object so it's safe to lock (this) as long as our
            // code doesn't use the collocated request handler as a lock. The lock here is useful to ensure
            // the protocol trace and observer call is output or called in the same order as the request ID
            // is allocated.
            lock (_mutex)
            {
                if (!oneway)
                {
                    requestId = ++_requestId;
                }

                if (observer != null)
                {
                    childObserver = observer.GetCollocatedObserver(_adapter, requestId, outgoingRequest.Size);
                    childObserver?.Attach();
                }

                if (_adapter.Communicator.TraceLevels.Protocol >= 1)
                {
                    ProtocolTrace.TraceCollocatedFrame(_adapter.Communicator,
                                                       (byte)Ice1Definitions.FrameType.Request,
                                                       requestId,
                                                       outgoingRequest);
                }
            }

            Task <OutgoingResponseFrame> task;

            if (_adapter.TaskScheduler != null || !synchronous || oneway || _reference.InvocationTimeout > 0)
            {
                // Don't invoke from the user thread if async or invocation timeout is set. We also don't dispatch
                // oneway from the user thread to match the non-collocated behavior where the oneway synchronous
                // request returns as soon as it's sent over the transport.
                task = Task.Factory.StartNew(() =>
                {
                    progress.Report(false);
                    return(DispatchAsync(outgoingRequest, requestId, cancel));
                },
                                             cancel,
                                             TaskCreationOptions.None,
                                             _adapter.TaskScheduler ?? TaskScheduler.Default).Unwrap();

                if (oneway)
                {
                    childObserver?.Detach();
                    return(IncomingResponseFrame.WithVoidReturnValue(outgoingRequest.Protocol,
                                                                     outgoingRequest.Encoding));
                }
            }
            else // Optimization: directly call DispatchAsync
            {
                Debug.Assert(!oneway);
                progress.Report(false);
                task = DispatchAsync(outgoingRequest, requestId, cancel);
            }

            try
            {
                OutgoingResponseFrame outgoingResponseFrame = await task.WaitAsync(cancel).ConfigureAwait(false);

                var incomingResponseFrame = new IncomingResponseFrame(
                    outgoingRequest.Protocol,
                    VectoredBufferExtensions.ToArray(outgoingResponseFrame.Data));

                if (_adapter.Communicator.TraceLevels.Protocol >= 1)
                {
                    ProtocolTrace.TraceCollocatedFrame(_adapter.Communicator,
                                                       (byte)Ice1Definitions.FrameType.Reply,
                                                       requestId,
                                                       incomingResponseFrame);
                }

                childObserver?.Reply(incomingResponseFrame.Size);
                return(incomingResponseFrame);
            }
            catch (Exception ex)
            {
                childObserver?.Failed(ex.GetType().FullName ?? "System.Exception");
                throw;
            }
            finally
            {
                childObserver?.Detach();
            }
        }
示例#58
0
 public Synchronizer(string localRepositoryPath, ProjectFolderConfiguration project, IProgress progress)
 {
     _progress              = progress;
     _project               = project;
     _localRepositoryPath   = localRepositoryPath;
     _handlers              = ChorusFileTypeHandlerCollection.CreateWithInstalledHandlers();
     ExtraRepositorySources = new List <RepositoryAddress>();
     ExtraRepositorySources.Add(RepositoryAddress.Create(RepositoryAddress.HardWiredSources.UsbKey, "USB flash drive", false));
 }
示例#59
0
        public async Task <ChatLog> GetChatLogAsync(AuthToken token, Channel channel,
                                                    DateTimeOffset?after = null, DateTimeOffset?before = null, IProgress <double>?progress = null)
        {
            // Get guild
            var guild = !string.IsNullOrWhiteSpace(channel.GuildId)
                ? await GetGuildAsync(token, channel.GuildId)
                : Guild.DirectMessages;

            // Get the chat log
            return(await GetChatLogAsync(token, guild, channel, after, before, progress));
        }
        public static async Task <IReadOnlyList <ExecutedQuery> > ExecuteQueriesAsync(this QueryService queryService, IReadOnlyList <Query> queries,
                                                                                      IProgress <double> progress = null)
        {
            var result = new List <ExecutedQuery>(queries.Count);

            for (var i = 0; i < queries.Count; i++)
            {
                var executedQuery = await queryService.ExecuteQueryAsync(queries[i]);

                result.Add(executedQuery);

                progress?.Report((i + 1.0) / queries.Count);
            }

            return(result);
        }