static void Main(string[] args)
        {
            Perfmon _perf   = new Perfmon();
            int     counter = 0;

            // init to start capturing
            _perf.GetCpuTotal();
            do
            {
                Console.WriteLine(_perf.GetCpuTotal());
                counter++;
            } while (counter <= 25);
        }
예제 #2
0
        public void Run(string[] args)
        {
            try
            {
                int port = 3434;
                if (args.Length > 0)
                    port = int.Parse(args[0]);
                int interval = 15;
                if (args.Length > 1)
                    interval = int.Parse(args[1]);

                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
                listener.Start();

                Perfmon perfmon = new Perfmon();

                SharedSampleListeners sharedlisteners = new SharedSampleListeners();

                foreach (String n in Categories())
                    perfmon.AddCategory(n, sharedlisteners);

                Object lockObject = new Object();
                Timer timer = new Timer((object o) =>
                {
                    if (!isPolling)
                    {
                        lock (lockObject)
                        {
                            isPolling = true;
                            try
                            {
                                try
                                {
                                    perfmon.Poll();
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(perfmon.Host() + " could not be polled." + e.Message);
                                }
                            }
                            finally { isPolling = false; }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Exhausted at " + new DateTime());
                    }
                }, null, 0, interval * 1000);
                try
                {

                    Console.WriteLine("Listening");

                    while (!stop.WaitOne(100))
                    {
                        while (listener.Pending())
                        {
                            TcpClient client = listener.AcceptTcpClient();
                            clients.Add(client);

                            client.SendTimeout = 5000;

                            try
                            {
                                var sw=TextWriter.Synchronized(new StreamWriter(client.GetStream()) { AutoFlush = true });
                                SampleListenerFilter sl = new SampleListenerFilter(new Regex(".*"), new Regex(".*"), new Regex(".*"), new Regex(".*"), new StructSampleListener(sw), false);

                                sharedlisteners.Add(sl);
                                Thread t = new Thread(new CommandLoop(client, clients, sw, sl).commandLoop);
                                t.IsBackground = true;
                                t.Start();
                                //Appearently, we dont need to cleanup the handle in .NET!?!?
                            }
                            catch (Exception)
                            {
                                clients.Remove(client);
                                client.Close();

                            }
                        }
                    }

                }
                finally
                {
                    listener.Stop();
                    timer.Dispose();
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }