Ejemplo n.º 1
0
 /// <summary>
 /// Repeatedly yields a lazy invocation attempt of the factory as an enumerable.
 /// </summary>
 /// <param name="default">The result value when not successful.</param>
 public static IEnumerable <Lazy <Attempt <T> > > Get <T>(Func <T> factory, T @default = default(T))
 {
     while (true)
     {
         yield return(new Lazy <Attempt <T> >(() => Attempt.Get(factory, @default)));
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns files that haven't changed over a period of time.
        /// </summary>
        /// <param name="source">Files to check.</param>
        /// <param name="interval">Time to wait between checking the files.</param>
        /// <param name="cancellationToken">Token to cancel the wait between checks.</param>
        /// <returns>Files that haven't changed.</returns>
        public static FileInfo[] GetUnchangedFiles(this IEnumerable <FileInfo> source, TimeSpan interval,
                                                   CancellationToken cancellationToken = default(CancellationToken))
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            var e = source.Select(f => new
            {
                FileInfo    = f,
                HashAttempt = Attempt.Get(() => f.ComputeFileHash())
            }).Where(f => f.HashAttempt.Succeeded);

            // ReSharper disable PossibleMultipleEnumeration
            var files = e.ToArray();

            try
            {
                Task.Delay(interval, cancellationToken)
                // ReSharper disable once MethodSupportsCancellation
                .Wait();
            }
            catch (AggregateException ex)
            {
                ex.Handle(x => x is OperationCanceledException);
            }
            cancellationToken.ThrowIfCancellationRequested();
            return(e.Join(files, x => x.FileInfo.FullName, x => x.FileInfo.FullName, Tuple.Create)
                   .Where(x =>
            {
                try
                {
                    return x.Item1.FileInfo.Length == x.Item2.FileInfo.Length &&
                    x.Item1.FileInfo.LastWriteTimeUtc == x.Item2.FileInfo.LastWriteTimeUtc &&
                    x.Item1.HashAttempt.Value == x.Item2.HashAttempt.Value;
                }
                catch (IOException)
                {
                    return false;
                }
            })
                   .Select(x => x.Item1.FileInfo).ToArray());
            // ReSharper restore PossibleMultipleEnumeration
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Invokes the factory, using the source as input, suppressing any thrown exception.
 /// </summary>
 /// <param name="default">The result value when not successful.</param>
 public static Attempt <TResult> AttemptGet <TSource, TResult>(this TSource source, Func <TSource, TResult> factory,
                                                               TResult @default = default(TResult))
 {
     return(Attempt.Get(() => factory(source)));
 }
Ejemplo n.º 4
0
        public static Task <T> StartNew <T>(this TaskFactory taskFactory, Func <T> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
        {
            var taskScheduler = Attempt.Get(TaskScheduler.FromCurrentSynchronizationContext).Value ?? TaskScheduler.Default;

            return(taskFactory.StartNew(function, cancellationToken, creationOptions, taskScheduler));
        }