Exemplo n.º 1
0
        /// <summary>
        /// A simpler alternative to the irritatingly useless FileSystemWatcher
        /// </summary>
        /// <param name="file">The file to monitor</param>
        /// <param name="refreshPeriod">The refresh period.</param>
        /// <param name="scheduler">The scheduler.</param>
        /// <returns></returns>
        public static IObservable <FileNotification> WatchFile(this FileInfo file, TimeSpan?refreshPeriod = null,
                                                               IScheduler scheduler = null)
        {
            return(Observable.Create <FileNotification>(observer =>
            {
                var refresh = refreshPeriod ?? TimeSpan.FromMilliseconds(250);
                scheduler = scheduler ?? Scheduler.Default;

                FileNotification notification = null;
                return scheduler.ScheduleRecurringAction(refresh, () =>
                {
                    try
                    {
                        notification = notification == null
                            ? new FileNotification(file)
                            : new FileNotification(notification);

                        observer.OnNext(notification);
                    }
                    catch (Exception ex)
                    {
                        notification = new FileNotification(file, ex);
                        observer.OnNext(notification);
                    }
                });
            }).DistinctUntilChanged());
        }
Exemplo n.º 2
0
        private FileSegmentCollection Accumulator(FileSegmentCollection previous, FileNotification current)
        {
            if (previous == null || previous.FileLength == 0)
            {
                _info = (FileInfo)current;

                var fileSegments = LoadSegments().ToArray();
                return(new FileSegmentCollection(_info, fileSegments, current.Size, true));
            }

            _info = (FileInfo)current;
            return(new FileSegmentCollection(previous, _info, LoadSegments().ToArray(), current.Size, false));
        }