Exemple #1
0
        public static void Process(string coonnectionString, Filter filter, string logFile, CancellationToken ct)
        {
            try
            {
                ConnectorBase Connector = ConnectorFactory.CreateConnector(coonnectionString);
                Connector.Connect();

                Package package;

                if (logFile != null && logFile != string.Empty)
                {
                    logFileFileStream   = new FileStream(logFile, FileMode.OpenOrCreate);
                    logFileStreamWriter = new StreamWriter(logFileFileStream);
                }
                bool first         = true;
                int  packagesCount = 0;
                var  sw            = new Stopwatch();

                while (true)
                {
                    Thread.Sleep(1);
                    while (Connector.Receive(out package))
                    {
                        packagesCount++;
                        if (first)
                        {
                            sw.Start();
                            first = false;
                        }
                        try
                        {
                            bool processMessage = filter.applyFilter(package);
                            if (processMessage)
                            {
                                package.PrintToConsole(packagesCount, ConsoleColor.Green);

                                if (logFileStreamWriter != null)
                                {
                                    logFileStreamWriter.WriteLine("{0}", package.ToString());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine(string.Format("SendMessages Exception: {0}", ex.Message));
                            Console.ForegroundColor = ConsoleColor.Green;
                            return;
                        }
                        if (packagesCount == 1000)
                        {
                            sw.Stop();
                            Console.WriteLine("{0} Packages in {1} Milliseconds ~ {2} packages per millisecond and {3} packages per second.", packagesCount, sw.ElapsedMilliseconds, packagesCount / sw.ElapsedMilliseconds, (packagesCount / sw.ElapsedMilliseconds) * 1000);
                        }
                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(e.Message);
                Console.ForegroundColor = ConsoleColor.Green;
            }

            return;
        }