コード例 #1
0
        private static void Generator_OnPrimesWrittenToFile(object generator, PrimesWrittenToFileArgs args)
        {
            var format  = "{0}. Wrote primes #{1} to #{2} to file at {3} (generation time: {4} sec).";
            var message = String.Format(format, args.FileIndex, args.StartPrimeIndex + 1, args.EndPrimeIndex, args.WriteTime, args.GenerationDuration.TotalSeconds);

            Console.WriteLine(message);
        }
コード例 #2
0
 /// <summary>
 /// Handles the OnPrimesWrittenToFile event for the object handling result files.
 /// </summary>
 /// <param name="handler">The object writing prime numbers to file.</param>
 /// <param name="args">The information about the event.</param>
 private void FileHandler_OnPrimesWrittenToFile(object handler, PrimesWrittenToFileArgs args)
 {
     OnPrimesWrittenToFile?.Invoke(handler, args);
 }
コード例 #3
0
        /// <summary>
        /// Writes a number of primes to a file.
        /// </summary>
        /// <param name="primesToWrite">The primes to write to a file.</param>
        public void WritePrimesToFile(List <BigInteger> primesToWrite)
        {
            KeyValuePair <int, string> lastFile;
            int          amountOfLines, startIndex;
            StreamWriter stream;

            prepareFileForWriting();
            openStream();

            try
            {
                for (int i = 0; i < primesToWrite.Count; i++)
                {
                    stream.WriteLine(primesToWrite[i]);
                    amountOfLines++;

                    var fileIsFilled         = (amountOfLines == Configuration.NumberOfPrimesInFile);
                    var fileIsOverfilled     = (amountOfLines > Configuration.NumberOfPrimesInFile);
                    var hasMorePrimesToWrite = (i < primesToWrite.Count - 1);

                    if (fileIsFilled && hasMorePrimesToWrite)
                    {
                        addEmptyResultFile(lastFile.Key + 1);

                        closeStream();
                        openStream();
                    }
                    else if (fileIsOverfilled)
                    {
                        createToManyLinesException(lastFile);
                    }
                }
            }
            finally
            {
                closeStream();
            }

            void openStream()
            {
                //Fetch the last file.
                lastFile = ResultFiles.Last();

                //Find out how many rows there are already written.
                amountOfLines = File.Exists(lastFile.Value) ? File.ReadLines(lastFile.Value).Count() : 0;

                //Open the stream.
                stream = new StreamWriter(lastFile.Value, true);

                //Update the index of the first prime in the file.
                startIndex = (ResultFiles.Count - 1) * Configuration.NumberOfPrimesInFile + amountOfLines;
            }

            void closeStream()
            {
                //Close the stream.
                stream.Close();

                //Calculate how long the writing took.
                var duration = DateTime.Now - LastFileWrite;

                LastFileWrite = DateTime.Now;

                //Inform the subscriber that the primes was written to a file.
                var endIndex = startIndex + primesToWrite.Count;
                var args     = new PrimesWrittenToFileArgs(lastFile.Key, startIndex, endIndex, DateTime.Now, duration);

                OnPrimesWrittenToFile?.Invoke(this, args);
            }
        }