Results 1 to 4 of 4

Thread: [RESOLVED]process.start and process.waitforexit

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Posts
    43

    [RESOLVED]process.start and process.waitforexit

    this is probably pretty simple but i'm not seeing it. i'm doing the following

    Code:
     Process.Start("C:\Program Files\7-Zip\7z", "x" & " " & G.StagingDirectory & " " & "-o" & G.StagingDirectory)
                'goes through directory and deletes the .mdoc files after they have been extracted
                Dim filecount As Integer = 0
                For Each filename As String In IO.Directory.GetFiles(G.StagingDirectory, "*.mdoc")
                    IO.File.Delete(filename)
                    filecount = filecount + 1
                Next
    the problem is it deletes the files before they can be extracted. i tried to use system.threading.thread.sleep(lots o seconds)


    how can i make the program wait for the process to finish?

    as always any help is welcomed.
    Last edited by bsanders; Apr 25th, 2013 at 10:04 AM.

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

    Re: [RESOLVED]process.start and process.waitforexit

    You've answered your own question: by calling Process.WaitForExit. As the name suggests, Process.Start just starts a new process. Your code will then carry on while that process executes. If you don't want your code to carry on until the process finishes executing then you call Process.WaitForExit. You need to call it on the Process object returned by Process.Start though. That might look like this:
    Code:
    Dim proc = Process.Start(fileName, arguments)
    
    proc.WaitForExit()
    or, more succinctly, like this:
    Code:
    Process.Start(fileName, arguments).WaitForExit()
    Note that WaitForExit is a blocking call, which means that your UI, if you have one, will freeze. If you want to avoid that then handle the Exited event of the Process instead and put the rest of your code in the event handler.
    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 2013
    Posts
    43

    Re: [RESOLVED]process.start and process.waitforexit

    yeah, thank you! i had to re-work my proc start but now it's working perfectly.

    i tried to change this to a resolved thread but i'm a n00b and i'm not seeing that anywhere.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,379

    Re: [RESOLVED]process.start and process.waitforexit

    Quote Originally Posted by bsanders View Post
    i tried to change this to a resolved thread but i'm a n00b and i'm not seeing that anywhere.
    At the top of this webpage, you should see thread tools with a down arrow to the right of it, click on that. At the bottom of that menu, you should see Mark Thread As Resolved.

    Name:  thread tools.png
Views: 13424
Size:  11.8 KB
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

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