Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var options = new CommandlineOptions();
            if (!Parser.Default.ParseArguments(args, options))
            {
                //nonparsable arguments
                return;
            }

            var directoryPath = Path.GetFullPath(options.Path);
            var filePaths = Directory.EnumerateFiles(directoryPath);

            ulong count = 0;
            Parallel.ForEach(filePaths, path =>
            {

                var file = new TextFile(path);
                file.Parse();
                Console.WriteLine(path+": "+file.Words);
                lock (_locker)
                {
                    count += file.Words;
                }

            });

            Console.WriteLine();
            Console.WriteLine("all words in all files: " + count);

            Console.ReadKey();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var options = new CommandlineOptions();
            Parser.Default.ParseArguments(args, options);

            if(  options.Port !=0 )
            {
                StartServer(options.Port);
                return;
            }

            var directoryPath = Path.GetFullPath(options.Path);
            var filePaths = Directory.EnumerateFiles(directoryPath);

            ulong count = 0;
            var usedPorts = new List<int>();
            var random = new Random();
            Parallel.ForEach(filePaths, path =>
            {
                //processes are great, see Google Chrome
                var self = Process.GetCurrentProcess().MainModule.FileName;
                self = self.Replace(".vshost", "");
                ProcessStartInfo info = new ProcessStartInfo(self);
                int port = 12345;
                lock (_locker)
                {
                    do
                    {
                        port = random.Next(1025, 65530);
                    }
                    while (usedPorts.Contains(port));

                    info.Arguments =  "--port=" + port;
                    usedPorts.Add(port);
                }

                info.UseShellExecute = true;//just to show blinking windows
                var serverProcess = Process.Start(info);

                ChannelFactory<IServer> httpFactory =
                        new ChannelFactory<IServer>(
                          new BasicHttpBinding(),
                          new EndpointAddress(
                            "http://localhost:"+port+"/Server"));

                var server = httpFactory.CreateChannel();
                ulong words = 0;
                try
                {
                    words = server.CountWords(path);
                }
                //happens sometimes, e.g. chosen port is taken by another app..
                catch (Exception e)
                {
                    //Console.WriteLine(e.ToString());
                }
                Console.WriteLine(path + ": " + words);
                lock (_locker)
                {
                    count += words;
                }

                serverProcess.Kill();
            });

            Console.WriteLine();
            Console.WriteLine("all words in all files: " + count);

            Console.ReadKey();
        }