Running Windows Command line by line

asokfair

Member
Joined
Jan 13, 2013
Messages
22
Programming Experience
Beginner
Hi,

I am new to DotNet . Currently i am developing an windows application which needs to run a batch file commands.
Long back i have used VC++ code to run an windows cmd using System("...")
likewise i want to know, in what way we can run an windows command in VB.Net
can anyone help me?

Thanks in Advance
 
Hi ,
I am using the same method to run an batch file... by creating process
batch file will takes few seconds to finish the process. until i have to wait in a loop
so i used the code like,

Dim myprc As Process = New Process
myprc.StartInfo.FileName = Project_path

myprc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myprc.StartInfo.CreateNoWindow = True
myprc.Start()
myprc.WaitForExit()
myprc.Close()


In here what problem i noticed was , while running
i cannot minimize the window
if i open any other window my application get hang
how to solve this issue..?
i want to handle the App in smooth manner
 
asokfair, that's because you're using WaitForExit() which locks your application until the process has finished, so you wont be able to do things like minimize your application.
What you should do is declare the Process class at the top of your form using the WithEvents keyword, then in a button's click event start the process similar to what you have shown here. Then use the Exited event of the process to know when the other process has completed. This will allow your form to stay responsive while it's waiting for the process to finish.
 
Any other way is there to avoid
myprc.WaitForExit() ...?
because if i run my app in windows xp its getting struck highly
or else can you give a sample code for that?

actually i have set of commands in Batch file.. now with the help of Batch file iam running. Is it possible to execute the same commands from VB.net itself ..?
 
Any other way is there to avoid
myprc.WaitForExit() ...?
because if i run my app in windows xp its getting struck highly
or else can you give a sample code for that?

actually i have set of commands in Batch file.. now with the help of Batch file iam running. Is it possible to execute the same commands from VB.net itself ..?
Yes, everything you can do in a batch, command, or vbs file you can do in .Net.

Example:
Imports System.Diagnostics

Friend Class Form1

    Private WithEvents m_Prc As Process
    Private m_PrcWorking As Boolean = False

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Not m_PrcWorking Then
            m_PrcWorking = True
            Dim stInf As New ProcessStartInfo(Project_path)
            stInf.WindowStyle = ProcessWindowStyle.Hidden
            stInf.CreateNoWindow = True
            m_Prc = Process.Start(stInf)
        End If
    End Sub

    Private Sub m_Prc_Exited(sender As Object, e As System.EventArgs) Handles m_Prc.Exited
        m_PrcWorking = False
        m_Prc.Dispose()
    End Sub

End Class
 
Thank you so much.. the code works ! but if i write the message box inside the event its not displaying.

"" Public Sub m_Prc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Prc.Exited
m_PrcWorking = False
MsgBox("Completed")
m_prc.Dispose()
End Sub "

The MsgBox not displaying after the complete of process ? any solutions?

and one more thing , Once the flag m_PrcWorking becomes FALSE i want to start next process. so how to call one function based on Flag changes?
 
sorry John I am not able to get the idea.. Any other way to do this..?

My question is I tried the following way to display the message box... but it not works why?

Public Sub m_Prc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Prc.Exited
m_PrcWorking = False
MsgBox("Ends")
m_prc.Dispose()
End Sub

Public Sub m_Prc_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_prc.Disposed
MsgBox("Ends")
End Sub
 
sorry John I am not able to get the idea.. Any other way to do this..?

My question is I tried the following way to display the message box... but it not works why?

Public Sub m_Prc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Prc.Exited
m_PrcWorking = False
MsgBox("Ends")
m_prc.Dispose()
End Sub

Public Sub m_Prc_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_prc.Disposed
MsgBox("Ends")
End Sub
You need to set the process' EnableRaiseEvents to True right after it's created:
Imports System.Diagnostics

Friend Class Form1

    Private Project_path As String

    Private WithEvents m_Prc As Process
    Private m_PrcWorking As Boolean = False

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If Not m_PrcWorking Then
            m_PrcWorking = True
            Dim stInf As New ProcessStartInfo(Project_path)
            stInf.WindowStyle = ProcessWindowStyle.Hidden
            stInf.CreateNoWindow = True
            m_Prc = Process.Start(stInf)
            m_Prc.EnableRaisingEvents = True
        End If
    End Sub

    Private Sub m_Prc_Exited(sender As Object, e As System.EventArgs) Handles m_Prc.Exited
        m_PrcWorking = False
        m_Prc.Dispose()
        MessageBox.Show("Process has finsihed")
    End Sub

End Class
 
But, If i assign some value to process bar inside , throwing error !..?

Public Sub m_Prc_Exited(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Prc.Exited
m_PrcWorking = False
m_prc.Dispose()
ProcessBar1.Value=100
End Sub
 
Exited event is asynchronous, it is raised on a secondary thread by default, and you can't access UI controls from different threads. To make it raise the event on UI thread you can simply set SynchronizingObject property to the form reference (Me). Even easier if you only have one process is to drop a Process component from Toolbox onto form and configure it in designer, SynchronizingObject property is then automatically set for you.
 
Back
Top