Running commands via application

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 885 Views - Last Post: 03 May 2012 - 01:56 PM Rate Topic: -----

#1 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Running commands via application

Posted 28 April 2012 - 02:55 PM

Im creating a windows application, not a console application which will hopefully run commands within the program.

For testing purposes so far I have the following

Dim sCommand As String
        sCommand = "ping " & TextBox1.Text & " >d:\testing.txt"
        sCommand = "exit"
        Shell("cmd.exe /c" & sCommand)



My problem is this, the text file that is created only shows the first line

Pinging 172.172.5.25 with 32 bytes of data:

When it should be showing


Pinging 172.172.5.25 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 172.172.5.25:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

Is This A Good Question/Topic? 0
  • +

Replies To: Running commands via application

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 28 April 2012 - 07:56 PM

These days we are using the Process class instead of shell. Your command string does not contain the whole string instead it overwrites itself with the second statement.


Dim sCommand As String
sCommand = "ping " & TextBox1.Text & " >d:\testing.txt"
sCommand = "exit" 'sCommand = "exit" now


Dim sCommand As String
sCommand = "ping " & TextBox1.Text & " >d:\testing.txt "
sCommand &= "exit" 'concantenates the string


We also have the Network.Ping method or System.Net.NetWorkInformation.
Was This Post Helpful? 0
  • +
  • -

#3 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 29 April 2012 - 02:12 AM

Very sorry :( I'm very much a newbie to the whole vb scene.

So I spent most of last night looking at process class, the good news is I have working coding but the bad news is I believe I have extra coding because it makes my application hang for like 30 seconds.

If possible can you tell me if I can make my coding simpler so my application does not hang?

   Dim myProcess As Process = New Process
        Dim s As String
        myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.CreateNoWindow = True
        myProcess.StartInfo.RedirectStandardInput = True
        myProcess.StartInfo.RedirectStandardOutput = True
        myProcess.StartInfo.RedirectStandardError = True
        myProcess.Start()

        Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
        Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
        Dim sErr As System.IO.StreamReader = myProcess.StandardError

        sIn.AutoFlush = True
        sIn.Write("ping " & TextBox1.Text & " >d:\testing.txt" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)
        s = sOut.ReadToEnd()


        If Not myProcess.HasExited Then
            myProcess.Kill()
        End If
        sIn.Close()
        sOut.Close()
        sErr.Close()
        myProcess.Close()


Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 29 April 2012 - 06:18 AM

Sure, put all that code in a separate sub routine. Then use threading:

Dim processThread As New Threading.Thread(AddressOf <sub name without ()>)
processThread.IsBackground = True
processThread.Start()

Was This Post Helpful? 0
  • +
  • -

#5 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 29 April 2012 - 10:17 AM

Excellent work, just two things.

1, To do what I'm doing do you think I need all the coding in post 3? it seems a lot of code to do something simple - so basically could the code be done shorter?

2, I would like some text on the form to say that the command has finished, something like "done" so in the sub I did the following

       sIn.Write("ping " & TextBox1.Text & " >d:\testing.txt" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)
        Label1.Text = "DONE!!"



Now the application breaks on the label1 line with Cross-thread operation not valid: Control 'Label1' accessed from a thread other than the thread it was created on which im confused about.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 29 April 2012 - 11:35 AM

Time to learn about delegates. Since your using threads you need to have a delegate set the text property since other threads are not allowed to communicate directly with controls from the UI thread. What framework are you using?
Was This Post Helpful? 0
  • +
  • -

#7 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 29 April 2012 - 11:49 AM

Hello, I'm using VS2010 framework 4.0
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 29 April 2012 - 06:45 PM

This is called a lambda sub and is new to framework 4.0. Otherwise you have to use Delegates.

Me.Invoke(Sub() 
                'invoke raises the UI thread
                'now it is safe to access controls here
                 Label1.Text = "DONE!!"
                End Sub)

Was This Post Helpful? 0
  • +
  • -

#9 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 30 April 2012 - 12:45 AM

Thanks, where would you suggest to put this coding?

I have it in the following place however the label says done before the cmd has even finished and exited?

     sIn.AutoFlush = True
        sIn.Write("ping " & TextBox1.Text & " >d:\testing.txt" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)

        Me.Invoke(Sub()
                      'invoke raises the UI thread
                      'now it is safe to access controls here
                      Label1.Text = "DONE!!"
                  End Sub)


Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 30 April 2012 - 06:15 AM

Look at the process class and you will find 2 poss methods that may help you - hint your process is not done even though streams are.
Was This Post Helpful? 0
  • +
  • -

#11 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 30 April 2012 - 06:43 AM

ok many thanks.
Was This Post Helpful? 0
  • +
  • -

#12 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 03 May 2012 - 12:38 AM

One more thing Hawk,

If I was running a command what would you suggest is the best way to kill/cancel the command via the application?

I was thinking

 Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

        Dim processThread As New Threading.Thread(AddressOf RunCMD)
        processThread.IsBackground = True
        processThread.Abort()

    End Sub


Was This Post Helpful? 0
  • +
  • -

#13 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 03 May 2012 - 12:44 AM

The .abort didnt work <_<
Was This Post Helpful? 0
  • +
  • -

#14 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Running commands via application

Posted 03 May 2012 - 07:44 AM

You can't call abort if you didn't start the thread. The thread will only last as long as it is needed that is why it has WaitForInputIdle or WaitForExit methods. Process class.
Was This Post Helpful? 0
  • +
  • -

#15 vnewbie  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 71
  • Joined: 28-April 12

Re: Running commands via application

Posted 03 May 2012 - 08:27 AM

Thanks Hawk,

so the thread must be started before it can be aborted?

Something like the following....?

 Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click

        Dim processThread As New Threading.Thread(AddressOf RunCMD)
        processThread.IsBackground = True
        processThread.Start()
        processThread.Abort()

    End Sub



I have two buttons, a run button and a cancel button. The run button will have the above coding excluding the abort, the cancel button will have the above coding.

My biggest concern would be is that I would actually run it twice when aborting?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2