运行连续值多线程returns(ping程序)
running multiple threads with continous value returns (ping program)
美好的一天
我已经开始了一个项目,作为基础需要在 cmd "ping x.x.x.x -t" 中插入一个命令并且程序需要 return 输出直到指定参数
我正在考虑线程,因为我对多线程的理解有限,没有指导我无法继续
my ping class 接收字符串 ip,将其添加到预编译的命令字符串等。
我知道用于此用途的内置 ping class,但我更喜欢 "longer" 方法,因为我可以从中获得价值 information/experience
主要对象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";
}
和thread_handlerclass,管理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回复
我不想让任何人完成这个程序,我只是需要帮助处理 运行 个此 ping 的多个实例(或者可能 "threads"),因此
每个线程在运行 "StartPing()" 方法时,它应该 return 一个输出,例如简单地将 ping 输出到控制台,但它不会...
输出:
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
然后挂起
您应该使用 ping class 执行 ping。这 class 让您可以控制许多细节。
用 Process.Start()
调用 ping.exe
涉及过多的开销并使事情复杂化(正如您在尝试中所经历的那样)
你读取子进程的方式不对。这是一项异常复杂的任务。我不知道您为什么会收到此特定错误消息,但听起来它与流程标准输出重定向有关。例如你没有重定向标准错误。
我建议您使用投票最高的堆栈溢出片段之一,可以通过以下方式找到:site:whosebug.com process RedirectStandardOutput
。有数百个这样的问题。大多数解决方案都有细微的错误。
就这么简单,重定向标准输入和输出就做到了,只需一两次调整,瞧
美好的一天
我已经开始了一个项目,作为基础需要在 cmd "ping x.x.x.x -t" 中插入一个命令并且程序需要 return 输出直到指定参数
我正在考虑线程,因为我对多线程的理解有限,没有指导我无法继续
my ping class 接收字符串 ip,将其添加到预编译的命令字符串等。
我知道用于此用途的内置 ping class,但我更喜欢 "longer" 方法,因为我可以从中获得价值 information/experience
主要对象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";
}
和thread_handlerclass,管理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回复
我不想让任何人完成这个程序,我只是需要帮助处理 运行 个此 ping 的多个实例(或者可能 "threads"),因此
每个线程在运行 "StartPing()" 方法时,它应该 return 一个输出,例如简单地将 ping 输出到控制台,但它不会...
输出:
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
然后挂起
您应该使用 ping class 执行 ping。这 class 让您可以控制许多细节。
用 Process.Start()
调用 ping.exe
涉及过多的开销并使事情复杂化(正如您在尝试中所经历的那样)
你读取子进程的方式不对。这是一项异常复杂的任务。我不知道您为什么会收到此特定错误消息,但听起来它与流程标准输出重定向有关。例如你没有重定向标准错误。
我建议您使用投票最高的堆栈溢出片段之一,可以通过以下方式找到:site:whosebug.com process RedirectStandardOutput
。有数百个这样的问题。大多数解决方案都有细微的错误。
就这么简单,重定向标准输入和输出就做到了,只需一两次调整,瞧