Results 1 to 4 of 4

Thread: [2008] Command line from VB.Net?

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    49

    [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Command line from VB.Net?

    p.WaitForExit is your man.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    49

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [2008] Command line from VB.Net?

    Quote 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
  •  



Click Here to Expand Forum to Full Width