运行具有连续值返回的多个线程(ping程序)

时间:2015-06-15 07:05:06

标签: c# multithreading ping

美好的一天

我已经接受了一个项目,作为基础需要输入命令到cmd“ping x.x.x.x -t”并且程序需要返回输出直到指定的参数

我正在考虑线程,因为我对多线程的解释是有限的,我无法在没有指导的情况下继续

我的ping类接收字符串ip,将其添加到预编译的命令字符串等等。

我知道内置ping类用于此用途,但我更喜欢“更长”的方法,因为我将从此获得有价值的信息/经验

主要对象类: ping

class ping
{
    Process proc;
    ProcessStartInfo psi;
    string ip_address;
    bool bStop = false;        

    public ping(string ip)
    {
        ip_address = ip;
    }

    public void StartPing()
    {
        string cmdInput;
        psi = new ProcessStartInfo(Environment.GetEnvironmentVariable("COMSPEC"));
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = true;

        psi.UseShellExecute = false;
        proc = Process.Start(psi);

        proc.StandardInput.WriteLine("ping " + ip_address + " -t");        

        cmdInput = proc.StandardOutput.ReadLine();
        cmdInput = proc.StandardOutput.ReadLine();
        cmdInput = proc.StandardOutput.ReadLine();
        cmdInput = proc.StandardOutput.ReadLine();
        cmdInput = proc.StandardOutput.ReadLine();
        cmdInput = proc.StandardOutput.ReadLine();
        while (bStop == false)
        {
            cmdInput = proc.StandardOutput.ReadLine();
            Console.WriteLine(returnPing(cmdInput));
        }
        proc.Close();
    }

    private string returnPing(string cmdInput)
    {
        int start, end;
        string ping;
        if (cmdInput.IndexOf("Reply") != -1 && cmdInput.IndexOf("time") != -1)
        {
            start = cmdInput.IndexOf("time=") + 5;
            end = cmdInput.IndexOf("ms");
            ping = cmdInput.Substring(start, end - start);
            return ping;

        }
        else return "-1";
    }

thread_handler 类,它管理ping方法的多个实例,请不要使用console.writeline是一个临时输出,我将在以后更改

class thread_handler
{
    string[] ipList;
    private IList<Thread> threadList;

    public thread_handler(string[] ip)
    {
        ipList = ip;
        threadList = new List<Thread>();
        createThreads();            
    }

    private void createThreads()
    {
        foreach (string item in ipList)
        {
            ping NewPing = new ping(item);
            Thread newPingThread = new Thread(NewPing.StartPing);
            newPingThread.IsBackground = true;
            newPingThread.Name = string.Format("{0}", item);
            threadList.Add(newPingThread);
        }
        startAllThreads();
    }

    private void startAllThreads()
    {
        foreach (Thread item in threadList)
        {
            item.Start();
        }
    }
}

程序

class Program
{
    static string[] ipList;
    static void Main(string[] args)
    {
        ipList = new String[3];
        readData();
        sendData();       
    }

    private static void sendData()
    {
        thread_handler thIp = new thread_handler(ipList);
    }

    private static void readData()
    {
        //use sll with name defintions and ip address's with metadata
        ipList[0] = "10.0.0.2";
        ipList[1] = "telkom_exchange";
        ipList[2] = "goo.gl";

    }

这个程序的目标是(将来会改变gui)一个简单的控制台,其各自的尺寸可以不断ping某些ip地址(我们有禁用基础设施,因此程序用于提供信息),不断更新每个ping回复< / p>

我不希望任何人完成此程序,我只需要帮助运行此ping操作的多个实例(或可能是“线程”),因此

每个线程在运行“StartPing()”方法时,它应该返回一个输出,例如只需将ping输出到控制台,但它不会......

输出:

 The process tried to write to a nonexistent pipe. 
 The process tried to write to a nonexistent pipe.

然后挂起

3 个答案:

答案 0 :(得分:2)

您从子进程中读取的方式是不对的。这是一项令人惊讶的复杂任务。我不知道你为什么会得到这个特定的错误信息,但听起来它与进程标准输出重定向有关。例如,您没有重定向标准错误。

我建议您使用堆栈溢出的最高投票片段之一,由site:stackoverflow.com process RedirectStandardOutput找到。有数百个这样的问题。大多数解决方案都是错误的。

This is a good checklist.

答案 1 :(得分:0)

您应该使用ping class执行ping。该类允许您控制许多细节。

使用ping.exe调用Process.Start()涉及太多开销并使事情变得复杂(正如您在尝试中遇到的那样)

答案 2 :(得分:0)

尽可能简单,重定向标准输入和输出就可以做到,只需要一两个调整,瞧

相关问题