Run multiple threads with continuous return values ​​(ping program)

a good day

I reviewed a project that requires an insernt command in cmd "ping xxxx -t" as a basis, and the program needs to return the result to the specified parameter

I am looking at topics since my unreachable in multithreading is limited, I cannot proceed without a guide

my ping class which gets the ip string, adds it to the precompiled command line, etc.

I am aware of the built-in ping class for this use, but I would prefer to use the "longer" method as I would get useful information / experience from this

main object class: 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";
    }

      

and thread_handler that manages mutliple instances of the ping method, please don't console.writeline - this is a temporary output that I will modify in the future

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();
        }
    }
}

      

Program

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 with metadata
        ipList[0] = "10.0.0.2";
        ipList[1] = "telkom_exchange";
        ipList[2] = "goo.gl";

    }

      

The purpose of this program (with gui changes in the future) is for a simple console with appropriate parameters to constantly ping a specific IP address (we have a forbidden infrastructure, so the program is for informational purposes only), constantly updating on every ping response

I don't want anyone to end up with this program, I just need help running multiple instances (or maybe "streams") of this pingang, thus

each thread, when it runs the "StartPing ()" method, it should return output, for example. just print ping to the console, but it doesn't ...

Output:

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

      

then freezes

+3


source to share


3 answers


The way you read from the child process is wrong. This is an amazingly difficult task. I don't know why you are getting this particular error, but it looks like it has to do with redirecting the output of the process. For example, you haven't redirected standard error.

I suggest you use one of the best voted fragments, which can be found at: site:stackoverflow.com process RedirectStandardOutput

. There are hundreds of such questions. Most decisions are subtly flawed.



This is a good checklist.

+2


source


To ping, you must use the ping class . This class allows you to control many details.



Calling ping.exe

with Process.Start()

involves too much overhead and complicates things (as you experienced in your attempt)

0


source


Anyway, standard input and output redirection did it with tweak or two, voila

0


source







All Articles