コード例 #1
0
ファイル: NinjectCommon.cs プロジェクト: Stonefacer/TestTask
 public static NinjectCommon Instance()
 {
     if (_instance == null)
     {
         _instance = new NinjectCommon();
     }
     return(_instance);
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: Stonefacer/TestTask
        private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            PrintMessage("Exiting...");
            var ninject  = NinjectCommon.Instance();
            var settings = ninject.Kernel.Get <ApplicationSettings>();

            // request termination of worker threads
            settings.Cancelation.Cancel();
            // cancel process termination
            e.Cancel = true;
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: Stonefacer/TestTask
        /// <summary>
        /// Creates array of threads to process a file
        /// </summary>
        /// <returns>created array of threads</returns>
        private static Thread[] CreateWorkers()
        {
            var ninject  = NinjectCommon.Instance();
            var settings = ninject.Kernel.Get <ApplicationSettings>();
            var threads  = new Thread[settings.ThreadsCount];

            for (int i = 0; i < settings.ThreadsCount; i++)
            {
                threads[i] = new Thread(ThreadWorker);
                threads[i].Start();
                threads[i].Priority = ThreadPriority.Lowest;
            }
            return(threads);
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: Stonefacer/TestTask
        /// <summary>
        /// Start method for each thread
        /// </summary>
        private static void ThreadWorker()
        {
            var ninject  = NinjectCommon.Instance();
            var settings = ninject.Kernel.Get <ApplicationSettings>();
            var worker   = ninject.Kernel.Get <Worker>();

            try
            {
                worker.ProcessFile(settings.Cancelation.Token);
            }
            catch (OperationCanceledException)
            {
                // cancelation requested
            }
            catch (Exception ex)
            {
                PrintFailedMessage(worker.CurrentLine, ex);
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: Stonefacer/TestTask
        /// <summary>
        /// Parse command line arguments
        /// </summary>
        /// <returns>returns true if sucess and false otherwise</returns>
        private static bool ParseCommandLineArguments(string[] args)
        {
            var ninject  = NinjectCommon.Instance();
            var settings = ninject.Kernel.Get <ApplicationSettings>();

            if (args.Length < 1)
            {
                PrintHelp();
                return(false);
            }
            for (var argumentIndex = 0; argumentIndex < args.Length; argumentIndex++)
            {
                var currentArgument = args[argumentIndex];
                if (currentArgument == "/t") // set count of threads
                {
                    if (args.Length < 3)     // one or two arguments missed
                    {
                        PrintHelp();
                        return(false);
                    }
                    argumentIndex++;
                    // check does threads count is correct number
                    int threadsCount;
                    if (!int.TryParse(args[argumentIndex], out threadsCount))
                    {
                        Console.WriteLine("Count of threads is not a number");
                        return(false);
                    }
                    if (threadsCount < 1)
                    {
                        Console.WriteLine("Count of threads must be greater than zero");
                        return(false);
                    }
                    settings.ThreadsCount = threadsCount;
                }
                else
                {
                    settings.Filename = currentArgument;
                }
            }
            return(true);
        }
コード例 #6
0
ファイル: Program.cs プロジェクト: Stonefacer/TestTask
        static void Main(string[] args)
        {
            var ninject  = NinjectCommon.Instance();
            var settings = ninject.Kernel.Get <ApplicationSettings>();

            // default settings
            settings.ThreadsCount = 1;
            settings.Cancelation  = new CancellationTokenSource();

            // parse command line
            if (!ParseCommandLineArguments(args))
            {
                return; // command line arguments cannot be parsed
            }
            // load settings from file
            var fileSettings = LoadSettingsFile();

            if (fileSettings == null)
            {
                PrintMessage("Settings cannot be loaded. Please check settings.xml file.");
                return;
            }
            ninject.BindFileSettings(fileSettings);

            // output settings
            Console.WriteLine("Filename: {0}", settings.Filename);
            Console.WriteLine("Connection settings: {0}", fileSettings.ConnectionString);
            Console.WriteLine("Threads count: {0}", settings.ThreadsCount);

            // check file existance
            var fileInfo = new FileInfo(settings.Filename);

            if (!fileInfo.Exists)
            {
                Console.WriteLine("File \"{0}\" doesn't exist", fileInfo.FullName);
                return;
            }

            // bind rest of interfaces
            ninject.BindDatabaseProvider(fileSettings.ConnectionString);
            ninject.BindLinesSource(fileInfo);

            // bind ctrl+c event so we could free resources correctly
            Console.CancelKeyPress += Console_CancelKeyPress;

            // create and start worker threads
            var threads = CreateWorkers();

            // save start date time for benchmarks
            var startDatetime = DateTime.Now;

            // wait for threads to exit
            var linesSource = ninject.Kernel.Get <ILinesSource>();

            for (var i = 0; i < threads.Length; i++)
            {
                while (!threads[i].Join(5000))
                {
                    // output progress any 5 seconds
                    Console.WriteLine("File progress: {0}/{1} ({2:F03}%); Current buffer progress: {3}/{4} ({5:F03}%)",
                                      linesSource.FilePosition, linesSource.FileTotalBytes, linesSource.FilePosition / (double)linesSource.FileTotalBytes * 100.0,
                                      linesSource.CurrentBufferPosition, linesSource.BufferSize, linesSource.CurrentBufferPosition / (double)linesSource.BufferSize * 100.0);
                }
            }

            var timeSpend = DateTime.Now - startDatetime;

            Console.WriteLine("Done in {0:00}:{1:00}:{2:00}.{3:000}", timeSpend.TotalHours, timeSpend.Minutes, timeSpend.Seconds, timeSpend.Milliseconds);
        }