Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NetWatcher"/> class.
        /// </summary>
        /// <param name="requestUri">The request URI to monitor for changes.</param>
        /// <param name="period">The time interval between periodic signaling for changes of provided <paramref name="requestUri"/>.</param>
        /// <param name="dueTime">The amount of time to delay before the associated <see cref="Watcher"/> starts signaling. Specify negative one (-1) milliseconds to prevent the signaling from starting. Specify zero (0) to start the signaling immediately.</param>
        /// <param name="checkResponseData">if set to <c>true</c>, a MD5 hash check of the response data is used to determine a change state of the resource; <c>false</c> to check only for the last modification of the resource.</param>
        /// <remarks>Monitors the provided <paramref name="requestUri"/> for changes in an interval specified by <paramref name="period"/>, determined by <paramref name="checkResponseData"/>.</remarks>
        public NetWatcher(Uri requestUri, TimeSpan dueTime, TimeSpan period, bool checkResponseData) : base(dueTime, period)
        {
            if (requestUri == null)
            {
                throw new ArgumentNullException(nameof(requestUri));
            }
            UriScheme scheme = UriSchemeConverter.FromString(requestUri.Scheme);

            switch (scheme)
            {
            case UriScheme.File:
            case UriScheme.Ftp:
            case UriScheme.Http:
            case UriScheme.Https:
                break;

            default:
                throw new ArgumentException("The provided Uri does not have a valid scheme attached. Allowed schemes for now is File, FTP, HTTP or HTTPS.", nameof(requestUri));
            }
            RequestUri        = requestUri;
            Scheme            = scheme;
            UtcCreated        = DateTime.UtcNow;
            CheckResponseData = checkResponseData;
            Signature         = SignatureDefault;
        }
        /// <summary>
        /// Starts and performs the necessary dependency tasks of this instance.
        /// </summary>
        /// <exception cref="System.ArgumentException">The provided Uri does not have a valid scheme attached. Allowed schemes for now is File, FTP or HTTP.;uris</exception>
        public override void Start()
        {
            List <NetWatcher> watchers = new List <NetWatcher>();

            foreach (Uri uri in Uris)
            {
                switch (UriSchemeConverter.FromString(uri.Scheme))
                {
                case UriScheme.File:
                case UriScheme.Http:
                case UriScheme.Https:
                    NetWatcher watcher;
                    NetWatcher tempWatcher = null;
                    try
                    {
                        tempWatcher          = new NetWatcher(uri, DueTime, Period, CheckResponseData);
                        tempWatcher.Changed += WatcherChanged;
                        watcher              = tempWatcher;
                        tempWatcher          = null;
                    }
                    finally
                    {
                        if (tempWatcher != null)
                        {
                            tempWatcher.Dispose();
                        }
                    }
                    watchers.Add(watcher);
                    break;

                default:
                    throw new InvalidOperationException("The provided Uri does not have a valid scheme attached. Allowed schemes for now is File, HTTP or HTTPS.");
                }
            }
            Watchers   = watchers.ToArray();
            UtcCreated = DateTime.UtcNow;
            SetUtcLastModified(UtcCreated);
        }