|
-
Jan 8th, 2009, 06:24 AM
#1
Thread Starter
Member
[2008] Command line from VB.Net?
Hello.
First off, i'm still rather new to Vb.Net but am enjoying learning it, I have this code to run a shutdown:
Code:
'// Thread Process for Shutdown Process
Private Sub BackgroundProcess()
If System.IO.File.Exists(dbfile) = True Then
Try
Dim shutdownCount As Integer
shutdownCount = 0
Dim shutdownmess As String = """This computer is now shutting down. If you wish to continue working on the computer please press cancel now."""
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=rc.mdb;")
'provider to be used when working with access database
cn.Open()
cmd = New OleDbCommand("select Comp from sheet1 WHERE Comp LIKE 'B10-%'", cn)
dr = cmd.ExecuteReader
While dr.Read()
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), "Shutdown has Started!")
Dim p As Process = New Process 'This is the name of the process we want to execute
p.StartInfo.FileName = ("cmd.exe")
p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardInput = True
p.Start()
p.StandardInput.WriteLine("psshutdown.exe \\" & dr(0) & " -u administrator -p **** -t 120 -f -k -c -m " & shutdownmess)
Dim compout As String
Threading.Thread.Sleep(5000)
compout = "Computer " + dr(0) + " has been sent the shutdown command."
Dim SROutput As System.IO.StreamReader = p.StandardOutput
While (p.HasExited = False)
Dim sLine As String = p.StandardOutput.ReadLine
If (Not String.IsNullOrEmpty(sLine)) Then
If sLine.Contains("scheduled to power off") Then
'We are on a worker thread so marshal the call to the UI thread.
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), sLine & vbCrLf)
End If
End If
End While
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), compout)
shutdownCount = shutdownCount + 1
Threading.Thread.Sleep(2000)
End While
Me.ListBox1.Invoke(New AddListBoxItemInvoker(AddressOf AddListBoxItem), "Shutdown is Complete!")
Catch exc As Exception
MessageBox.Show("Error: " & exc.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
End Try
Else
MessageBox.Show("Error: The database file" & dbfile & " does not exist!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
It runs ok. Currently it looks through a number of computer names and shuts them down. But sometimes it can take up to a minute for anything to be returned by command line if for example a computer is switched off. So what i'm trying to do is get it so it doesn't continue the loop UNTIL there has been something returned by command line. Some maybe to do with While p.HasExited or something? Is this possible?
thanks for any advice.
-
Jan 8th, 2009, 06:33 AM
#2
Re: [2008] Command line from VB.Net?
p.WaitForExit is your man.
-
Jan 8th, 2009, 07:14 AM
#3
Thread Starter
Member
Re: [2008] Command line from VB.Net?
Thanks. I'll try it out Assume i just use it like p.WaitForExit() where i want it to wait?
Thanks.
-
Jan 8th, 2009, 07:21 AM
#4
Re: [2008] Command line from VB.Net?
 Originally Posted by wwfc_barmy_army
Thanks. I'll try it out  Assume i just use it like p.WaitForExit() where i want it to wait?
Yes...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|