c# - running multiple threads with continous value returns (ping program) -


good day

i have taken project bases needs insernt command cmd "ping x.x.x.x -t" , program needs return output until specified parameter

i considering threads unterstanding in multithreading limited, unable continue without guidance

my ping class recieves string ip, adds precompiled command string, etc.

i aware of built in ping class use, prefer "longer" method since gain valueble information/experience 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 class, manages mutliple instances of ping method, please not console.writeline temporary output change in 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 name defintions , ip address's metadata         iplist[0] = "10.0.0.2";         iplist[1] = "telkom_exchange";         iplist[2] = "goo.gl";      } 

the aim of program (with gui changes in future) simple console respective dimensions ping ip address's (we have ban infrastructure, program informative purposes), updating on every ping reply

i not want finish program, require assistance running multiple instances (or maybe "threads") of pinging, thus

each thread runs "startping()" method, should return output, e.g. output ping console, doesnt...

output:

 process tried write nonexistent pipe.   process tried write nonexistent pipe. 

then hangs

the way read child process not right. surprisingly complicated task. don't know why getting specific error message sounds has process standard output redirection. example have not redirected standard error.

i suggest use 1 of top voted snippets of stack overflow, found by: site:stackoverflow.com process redirectstandardoutput. there hundreds of such questions. solutions subtly wrong.

this checklist.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -