private static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Please enter full path.");
                return;
            }
            string binName = args[0];

            if (args.Length >= 2)
            {
                string generate = args[1];
                if (generate == "-generateRandom")
                {
                    Console.WriteLine("Generating binary file \"{0}\" with {1} random numbers", binName, LengthOfFile);
                    BinaryGenerator.GenerateRandom(binName, LengthOfFile);
                }
                else if (generate == "-generate")
                {
                    int[] mass = { 2, 3, 5, 3, 5, 11 };
//          int[] mass = {2, 3, 4, 5, 6, 7, 3, 5, 7};
//          int[] mass = {2, 3, 10,5, 6, 7, 3, 5, 7};
//          int[] mass = {2, 3, 3, 5, 6, 7, 3, 5, 7};
//          int[] mass = {2, 3, 5, 3, 5, 11};
//          int[] mass = { 2, 3, 5, 2, 5, 11 };
                    Console.WriteLine("Generating binary file \"{0}\" with numbers: {1}", binName, string.Join(" ", mass));

                    BinaryGenerator.GenerateFromMassive(binName, mass);
                }
            }


            Thread workerThread = new Thread(Worker.DoWork);

            workerThread.Start(binName);

            Console.WriteLine("Press ESCAPE key to stop");
            while (workerThread.IsAlive)
            {
                ConsoleKeyInfo cki = Console.ReadKey();
                if (cki.Key == ConsoleKey.Escape && workerThread.IsAlive)
                {
                    Worker.Stop();
                    SequenceSearcher.GetResultCorrect();
                    break;
                }
            }
        }
Exemple #2
0
        public static void DoWork(object data)
        {
            var filePath = data as string;
            var buffer   = new byte[BufferSize];

            var taskStarted = DateTime.Now;

            Console.WriteLine("Task started at {0}", taskStarted);

            try
            {
                using (var fileStream = new BinaryReader(File.Open(filePath, FileMode.Open, FileAccess.Read)))
                {
                    int read;
                    while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0 && Running)
                    {
                        _position++;

                        Console.WriteLine("Processing chunk: {0}", _position);

                        var splittedList = SplitArrayBySixBytes(buffer, read);

                        var primeNumbers = splittedList
                                           .AsParallel()
                                           .AsOrdered()
                                           .Where(element => PrimeManager.IsPrime(element.Prime))
                                           .Select(x => x).WithCancellation(cts.Token).ToList();

                        SequenceSearcher.AddPrimes(primeNumbers);
                    }
                }
                SequenceSearcher.GetResultCorrect();
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Can't access file");
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("File not found");
            }
            catch (IOException)
            {
                Console.WriteLine("Can't read file");
            }
            catch (ThreadAbortException)
            {
                Console.WriteLine("Process was stopped");
            }
            catch (OperationCanceledException)
            {
                Console.WriteLine("Process was stopped");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed with exception:{0}", ex);
            }

            var taskEnded      = DateTime.Now;
            var timeDifference = taskEnded - taskStarted;

            Console.WriteLine("Task ended at {0}, done in {1}", taskEnded, timeDifference.ToString());
        }