コード例 #1
0
        private void CheckForUpdateAsync(object state)
        {
            if (state == null)
            {
                if (DateTime.UtcNow < _lastUpdateCheckedOn.AddDays(_checkUpdateIntervalDays))
                    return;
            }

            //update last check time
            _lastUpdateCheckedOn = DateTime.UtcNow;

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_checkUpdateURL);
                request.UserAgent = GetUserAgent();
                request.IfModifiedSince = _lastModifiedGMT;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                switch (response.StatusCode)
                {
                    case HttpStatusCode.OK:
                        _updateInfo = new UpdateInfo(response.GetResponseStream());
                        _lastModifiedGMT = DateTime.Parse(response.Headers["Last-Modified"]);

                        if (_updateInfo.IsUpdateAvailable(_currentVersion))
                        {
                            //update available, stop timer
                            _checkTimer.Change(Timeout.Infinite, Timeout.Infinite);

                            //raise event to user UI
                            RaiseEventUpdateAvailable();
                        }
                        else
                        {
                            //if manual check then raise event
                            if (state != null)
                                RaiseEventNoUpdateAvailable();
                        }

                        break;

                    default:
                        //if manual check then raise event
                        if (state != null)
                            RaiseEventNoUpdateAvailable();

                        break;
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse response = ex.Response as HttpWebResponse;

                if (response != null)
                {
                    switch (response.StatusCode)
                    {
                        case HttpStatusCode.NotModified:
                        case HttpStatusCode.NotFound:
                            //if manual check then raise event
                            if (state != null)
                                RaiseEventNoUpdateAvailable();
                            break;

                        default:
                            //if manual check then raise event
                            if (state != null)
                                RaiseEventUpdateError(ex);
                            break;
                    }
                }
                else
                {
                    //if manual check then raise event
                    if (state != null)
                        RaiseEventUpdateError(ex);
                }
            }
            catch (Exception ex)
            {
                //if manual check then raise event
                if (state != null)
                    RaiseEventUpdateError(ex);
            }

            if (state != null)
                _checkUpdate = null;
        }
コード例 #2
0
        private void CheckForUpdateAsync(object state)
        {
            if (state == null)
            {
                if (DateTime.UtcNow < _lastUpdateCheckedOn.AddDays(_checkUpdateIntervalDays))
                {
                    return;
                }
            }

            //update last check time
            _lastUpdateCheckedOn = DateTime.UtcNow;

            try
            {
                using (WebClientEx client = new WebClientEx())
                {
                    client.Proxy           = _proxy;
                    client.UserAgent       = GetUserAgent();
                    client.IfModifiedSince = _lastModifiedGMT;

                    byte[] responseData = client.DownloadData(_checkUpdateURL);

                    _updateInfo      = new UpdateInfo(new MemoryStream(responseData, false));
                    _lastModifiedGMT = DateTime.Parse(client.ResponseHeaders["Last-Modified"]);

                    if (_updateInfo.IsUpdateAvailable(_currentVersion))
                    {
                        //update available, stop timer
                        _checkTimer.Change(Timeout.Infinite, Timeout.Infinite);

                        //raise event to user UI
                        RaiseEventUpdateAvailable();
                    }
                    else
                    {
                        //if manual check then raise event
                        if (state != null)
                        {
                            RaiseEventNoUpdateAvailable();
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse response = ex.Response as HttpWebResponse;

                if (response != null)
                {
                    switch (response.StatusCode)
                    {
                    case HttpStatusCode.NotModified:
                    case HttpStatusCode.NotFound:
                        //if manual check then raise event
                        if (state != null)
                        {
                            RaiseEventNoUpdateAvailable();
                        }
                        break;

                    default:
                        //if manual check then raise event
                        if (state != null)
                        {
                            RaiseEventUpdateError(ex);
                        }
                        break;
                    }
                }
                else
                {
                    //if manual check then raise event
                    if (state != null)
                    {
                        RaiseEventUpdateError(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                //if manual check then raise event
                if (state != null)
                {
                    RaiseEventUpdateError(ex);
                }
            }

            if (state != null)
            {
                _checkUpdate = null;
            }
        }