예제 #1
0
        protected override void ProcessRecord() //Add this Process function method
        {
            if (MinHostGroup < 1)
            {
                MinHostGroup = 1;
            }

            MaxThreads = Math.Min(ComputerName.Length, MaxThreads);

            if (MaxThreads == 1) // if only 1 host or 1 thread max call pinscanasync on main thread
            {
                for (int i = 0; i < ComputerName.Length; i += MinHostGroup)
                {
                    WriteVerbose("Starting Ping Scan...");
                    List <PingScanner.PingResponse> plist = PingScanner.PingScan.PingScanAsync(ComputerName.Skip(i).Take(MinHostGroup).ToArray(), Timeout, NoDNSLookup).Result;
                    WriteVerbose("Writing objects now...");
                    foreach (var item in plist)
                    {
                        WriteObject(item);
                    }
                }
                return;
            }

            WriteVerbose(string.Format("MaxThreads:    {0}", MaxThreads));
            WriteVerbose(string.Format("MinHostGroup:  {0}", MinHostGroup));

            Thread[] ta  = new Thread[MaxThreads];
            object[] pra = new object[MaxThreads];
            int[]    d   = new int[MaxThreads];

            for (int i = 0; i < MaxThreads; i++)
            {
                pra[i] = null;
            }

            for (int i = 0; i < MaxThreads; i++)
            {
                // Console.WriteLine("I:{0}",i);
                int j = i;
                ta[j] = new Thread(delegate()
                {
                    // Console.WriteLine("ComputerName[ComputerSegment*j]: {0}", ComputerName[ComputerSegment * j]);
                    // Console.WriteLine("ComputerSgment: {0}    j: {1}", ComputerSegment, j);
                    // Console.WriteLine(j);
                    pra[j] = PingScan.PingScanAsync(ComputerName.Skip(MinHostGroup * j).Take(MinHostGroup).ToArray(), Timeout, NoDNSLookup).Result;
                });
                ta[i].Start();
            }

            for (int i = 0; i < MaxThreads; i++)
            {
                // ta[i].Start();
            }
            for (int i = 0; i < MaxThreads; i++)
            {
                ta[i].Join();
            }

            for (int i = 0; i < MaxThreads; i++)
            {
                List <PingResponse> pr = (List <PingResponse>)pra[i];
                // Console.WriteLine(pr.ElementAt(0).Destination);
                for (int j = 0; j < pr.Count(); j++)
                {
                    WriteObject(pr.ElementAt(j));
                }
            }
        }