| |

VerySource

 Forgot password?
 Register
Search
View: 707|Reply: 2

Problems with Process

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-6 13:30:01
| Show all posts |Read mode
private void button1_Click (object sender, System.EventArgs e)
   {
          int i = 1;
for (i = 1; i <254; i ++)
{
string pstr = "192.168.0." + i.ToString ();
string strRst = CmdPing (pstr);
              listBox1.Items.Add (pstr + "___" + strRst);
}
   }

private static string CmdPing (string strIp)
{
     Process p = new Process ();
     p.StartInfo.FileName = "cmd.exe";
     p.StartInfo.UseShellExecute = false;
     p.StartInfo.RedirectStandardInput = true;
     p.StartInfo.RedirectStandardOutput = true;
     p.StartInfo.RedirectStandardError = true;
     p.StartInfo.CreateNoWindow = true;
     string pingrst;
     p.Start ();
     p.StandardInput.WriteLine ("ping -n 1" + strIp);
     p.StandardInput.WriteLine ("exit");
     string strRst = p.StandardOutput.ReadToEnd ();
 
     if (strRst.IndexOf ("(0% loss)")! =-1)
pingrst = "connection";
      else if (strRst.IndexOf ("Destination host unreachable.")! =-1)
pingrst = "Unable to reach destination host";
      else if (strRst.IndexOf ("Request timed out.")! =-1)
pingrst = "Timeout";
      else if (strRst.IndexOf ("Unknown host")! =-1)
pingrst = "Unable to resolve host";
      else
pingrst = strRst;
       p.Close ();
      return pingrst;
}
When the above program is executed, the data in listBox1 is not displayed accordingly, but is displayed together after a long time. The time delay is unbearable. How can I display it accordingly?
Reply

Use magic Report

0

Threads

52

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-5-23 22:45:01
| Show all posts
Use thread processing for example
private void button6_Click (object sender, System.EventArgs e)
{
Thread cmdthread = new Thread (new ThreadStart (this.ThreadCmd));
cmdthread.IsBackground = true;
cmdthread.Start ();

}

private void ThreadCmd ()
{
int i = 1;
for (i = 1; i <254; i ++)
{
string pstr = "192.168.0." + i.ToString ();
string strRst = CmdPing (pstr);
listBox1.Items.Add (pstr + "___" + strRst);
}
}

private static string CmdPing (string strIp)
{
Process p = new Process ();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start ();
p.StandardInput.WriteLine ("ping -n 1" + strIp);
p.StandardInput.WriteLine ("exit");
string strRst = p.StandardOutput.ReadToEnd ();
 
if (strRst.IndexOf ("(0% loss)")! =-1)
pingrst = "Connect";
else if (strRst.IndexOf ("Destination host unreachable.")! =-1)
pingrst = "Unable to reach the destination host";
else if (strRst.IndexOf ("Request timed out.")! =-1)
pingrst = "Timeout";
else if (strRst.IndexOf ("Unknown host")! =-1)
pingrst = "Unable to resolve host";
else
pingrst = strRst;
p.Close ();
return pingrst;
}
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-5-30 18:30:01
| Show all posts
Thank you,:)
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list