Exemple #1
0
        //
        // methods
        //

        /// <summary>
        /// After call of this methods all existing and new requests
        /// will be thrown or returned as canceled
        /// </summary>
        public void Cancel()
        {
            // Lock the data access
            lock (this.dataLocker)
            {
                // If stete is pending
                if (this.state == FileLinesCheckerState.Pending)
                {
                    // Cancel the previous reader execution
                    this.linesReader.Cancel();
                }

                // Change the state
                this.state = FileLinesCheckerState.Canceled;

                // Remove reader link from roots
                this.linesReader = null;

                // Notify waiting threads about state changing
                Monitor.PulseAll(this.dataLocker);
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates new LinesReader object for the storage updating
        /// Sent notification to the other threads if update was not canceled
        /// </summary>
        /// <param name="notUsed">Not used parameter</param>
        private void LoadData(Object notUsed)
        {
            LinesReader currentReader = new LinesReader();

            // Stop previous LinesReader execution
            lock (this.dataLocker)
            {
                if (this.state == FileLinesCheckerState.Pending)
                {
                    // If State is pending - reader is exist
                    // Cancel previous reader execution
                    this.linesReader.Cancel();
                }
                else
                {
                    this.state = FileLinesCheckerState.Pending;
                }

                // Save link to the current reader for cancel
                this.linesReader = currentReader;
            }

            // Get new data from LinesReader
            IDictionary newData = null;

            try
            {
                using (Stream stream = new FileStream(this.fileName,
                                                      FileMode.Open, FileAccess.Read))
                {
                    // Load data
                    newData = currentReader.Read(stream);
                }
            }
            catch (Exception)
            {
                // TODO: Log exception
                // An unhandled exception causes to the program crach
            }

            // If process was not canseled - update the data
            lock (this.dataLocker)
            {
                // If the load process was not canceled or changed with another one
                if (!currentReader.IsCanceled)
                {
                    // If cancelation of the instance work was not requested
                    if (this.state != FileLinesCheckerState.Canceled)
                    {
                        // Renew the data and status
                        this.data  = newData;
                        this.state = this.data == null
                            ? FileLinesCheckerState.Error
                            : FileLinesCheckerState.Ready;
                    }

                    // Reader not needed more
                    this.linesReader = null;

                    // Notify other threads about data is available
                    Monitor.PulseAll(this.dataLocker);
                }
            }
        }