Results 1 to 15 of 15

Thread: [RESOLVED]How to make a command line wait?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    [RESOLVED]How to make a command line wait?

    In using the code I created below, I have came across an error
    The process cannot access the file because it is being used by another process.
    So my VB 8.0 2005 code is this...
    Private Sub Backup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Backup.Click
    On Error GoTo ClickErr
    Dim fileExists As Boolean
    fileExists = My.Computer.FileSystem.FileExists("C:\Program Files\Store2Office\DBBackup.zip")
    If fileExists Then
    My.Computer.FileSystem.RenameFile("C:\Program Files\Store2Office\DBBackup.zip", "OMC_Backup_DBBackup.zip")
    End If
    Shell("C:\Program Files\Store2Office\HK_Office.exe /backupdb")
    My.Computer.FileSystem.CreateDirectory("C:\Program Files\Store2Office\Archive")


    Dim folderExists As Boolean
    folderExists = My.Computer.FileSystem.DirectoryExists("C:\Program Files\Store2Office\Archive")
    If folderExists Then
    My.Computer.FileSystem.MoveFile("C:\Program Files\Store2Office\DBBackup.zip", "C:\Program Files\Store2Office\Archive\DBBackup.zip")
    End If
    MsgBox("Databases are backed up")
    ClickErr:
    MsgBox(Err.Description)
    End Sub
    So what I am thinking is that the HK_Office.exe is taking a little while to zip up the files for me. So my question is this. How can I make the next line to wait til that process is done before going to the next line to finish my request of moving the files?
    Last edited by davis2533; Aug 6th, 2006 at 08:00 PM. Reason: [RESOLVED]

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How to make a command line wait?

    Use the Process class instead of Shell in order to start the program. The process class has a .WaitForExit method that does just that, waits for the process to exit before executing the next line of code...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    Process class (program)?? instead of shell (program)?

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How to make a command line wait?

    Check out this recent thread for an example of running a program with parameters using the Process class...

    http://www.vbforums.com/showthread.php?t=420795

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    I am trying to get a full understanding how this works. so the code ou provided was...
    VB Code:
    1. Dim MyProcess As New Process
    2.         Dim MyStartInfo As New ProcessStartInfo
    3.         'filename of the program to run, best to use full path
    4.         MyStartInfo.FileName = "notepad.exe"
    5.         'arguments to pass into the program
    6.         MyStartInfo.Arguments = "my arguments"
    7.         'sets new startinfo object to MyProcess' StartInfo property
    8.         MyProcess.StartInfo = MyStartInfo
    9.         'starts the process
    10.         MyProcess.Start()
    MyStartInfo.FileName = "notepad.exe" is where I would put the program to zip my files right?
    now what is the arguments? Is that the next process?

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How to make a command line wait?

    The arguments is the string that you want to pass into the prog, in this case, "/backupdb", with the exe being "HK_Office.exe" (with the full path to the file)

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    Ok.. that explains it now. So what causes it to wait until that process is done before going to the next one?

  8. #8
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How to make a command line wait?

    well does this process exit automatically after that program and arguments are ran? If so, just add one line after you call MyProcess.Start, and that line would be "MyProcess.WaitForExit"

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    Thanks that worked just fine. I have another problem with in the same project but a diffenent part of the code.

    VB Code:
    1. Dim xCon As New OleDb.OleDbConnection
    2.         Dim xCom As New OleDb.OleDbCommand
    3.         Dim xAdap As New OleDb.OleDbDataAdapter(xCom)
    4.         Dim xComBuild As New OleDb.OleDbCommandBuilder(xAdap)
    5.         Dim ConnnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Store2Office\Emp.mdb;Mode=ReadWrite;Persist Security Info=False"
    6.  
    7.  
    8.         Dim emp As String = "Emp.mdb"
    9. [B]xCom.CommandText = "Delete * from charges where s_date<#" & TextBox1.Text & "#"[/B]
    10.         xCom.CommandText = "Delete * from clockchanges where indate<#" & TextBox1.Text & "#"
    11.         xCom.CommandText = "Delete * from clockdata where indate<#" & TextBox1.Text & "#"
    12.         xCom.CommandText = "Delete * from mhj_schedule where s_date<#" & TextBox1.Text & "#"
    13.         xCom.CommandText = "Delete * from schedule where indate<#" & TextBox1.Text & "#"
    14.         xCom.Connection = xCon
    15.         xCon.ConnectionString = ConnnectionString
    16.  
    17.         If xCon.State = ConnectionState.Closed Then
    18.             xCon.Open()
    19.         End If
    20.         xCom.ExecuteNonQuery()
    21.         xCon.Close()

    So my problem that I am getting is that the data is deleted with the first like that is in bold. But the rest of the tables are not touched. Would this be something like before? needing to setup a process or each line?

  10. #10
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: How to make a command line wait?

    Well all those lines are doing is changing the command text, you are only running it one time when you call .ExecuteNonQuery, and the command its going to run is the last thing it was changed to. You would have to execute it once for each command you wish to run I believe, although I haven't messed too much into the database side of things...

    Also, since this is an unrelated question, you might want to start a new thread about it in order for the database experts to see it...
    Last edited by gigemboy; Aug 6th, 2006 at 07:29 PM.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    So you mean like such?

    xCom.CommandText = "Delete * from charges where s_date<#" & TextBox1.Text & "#"
    xCom.ExecuteNonQuery()
    xCom.CommandText = "Delete * from clockchanges where indate<#" & TextBox1.Text & "#"
    xCom.ExecuteNonQuery()

    and every other line too?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    Ok.. I can repost that question..

    just a quicky though.. I created a cancel button.. but I can't find any referance as to how to make it close the window (program)

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    85

    Re: How to make a command line wait?

    the database question is on http://www.vbforums.com/showthread.p...22#post2573022

    thanks for helping me with this question thanks

  14. #14
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: [RESOLVED]How to make a command line wait?

    VB Code:
    1. Me.Close

    ...will close the form.

    VB Code:
    1. Application.Exit

    ...will exit the program.
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

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

    Re: [RESOLVED]How to make a command line wait?

    Please keep each topic to one thread and each thread to one topic. That way information is nice and easy to find for everyone. Asking multiple unrelated questions in the same thread makes information harder to find. Asking the same question in multiple threads makes life more difficult for those who are helping you, and that's something you should strive not to do.
    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

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