Results 1 to 8 of 8

Thread: [RESOLVED] Help on my Process coding

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Resolved [RESOLVED] Help on my Process coding

    Hi,

    I have some coding which just runs cmd commands.

    Code:
     Private Sub RunCMD()
            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("Insert Command Here" & 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()
    
        End Sub
    
        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()
    
    End Sub
    Now the code works perfectly fine but would it be possible to make the coding shorter or is there any extra coding which I can remove?
    Please mark threads as resolved once the problem has been solved.
    I apprecaite all your help/advice given

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Help on my Process coding

    Not really. What Id do is make a function to create the process object and set its StartInfo before returning the reference. This doesn't actually reduce the amount of code but it would make RunCMD smaller.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: Help on my Process coding

    thanks, one more thing if I wanted to add a cancel button which will kill any process and any cmd what would be the best way?

    If possible can you also give me an example?

    I was thinking.

    Code:
      Dim processThread As New Threading.Thread(AddressOf RunCMD)
            processThread.IsBackground = True
            processThread.Abort
    but this does not kill the command.
    Please mark threads as resolved once the problem has been solved.
    I apprecaite all your help/advice given

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Help on my Process coding

    What I'm suggesting is really simple. Basically what I meant is that you can split that procedure into two procedures. One to set up the process object and the other to do that other stuff you wrote:-
    vbnet Code:
    1. '
    2.     Public Function CreateCMDProcess() As Process
    3.  
    4.         Dim myProcess As Process = New Process
    5.  
    6.         myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
    7.         myProcess.StartInfo.UseShellExecute = False
    8.         myProcess.StartInfo.CreateNoWindow = True
    9.         myProcess.StartInfo.RedirectStandardInput = True
    10.         myProcess.StartInfo.RedirectStandardOutput = True
    11.         myProcess.StartInfo.RedirectStandardError = True
    12.  
    13.         Return myProcess
    14.     End Function
    You simply call this procedure to return the process thereby reducing the amount of code in RunCMD
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: Help on my Process coding

    ok many thanks, what are your thoughts about the cancel button? is that possible?
    Please mark threads as resolved once the problem has been solved.
    I apprecaite all your help/advice given

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Help on my Process coding

    Your thread doesn't run for long so I don't see a need for a cancel in this case. What are you trying to do there any way ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2004
    Location
    U.K
    Posts
    752

    Re: Help on my Process coding

    well some commands that I run will be collecting data, they can run for up to 1 hour at a time so I would need a way of canceling the process if possible.
    Please mark threads as resolved once the problem has been solved.
    I apprecaite all your help/advice given

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Help on my Process coding

    Then you should widen the scope of myProcess because as it is now, it will go out of scope leaving nothing to reference the process. Also, each process in Windows has its own set of threads so executing RunCMD on a different thread is a bit redundant and you don't seem to be waiting for the process to exit in that thread.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width