Jump to content

VB.NET Wait For Process...


Recommended Posts

I've created a program that, when clicking a button, runs another process (in this case, nLite) and waits for it to exit using the Process.WaitForExit() method.

The problem is, the GUI off my program freezes up while it waits for the procress to finish. Eg. Sending my program behind another problem and bringing it back again makes my program go white (needs to repaint itself but can't because it's waiting for the other process to finish).

I've tried using a new thread to run the process in and that leaves my process to repaint itself if needed BUT my program won't wait until the new thread is finished before doing everything else. That's the problem.

I've tried Thread.Join() but that has the same problem as initially stated. Any ideas on how to work with this problem cause I'm stuffed?

Link to comment
Share on other sites


I've been struggling with this problem as well. One thing to watch out for with the solution you gave is the possibility of putting your application in a race condition. Take a peek at Task Manager when you execute.

If I can find a better solution I'll be sure to post it here.

Link to comment
Share on other sites

Seems the easiest way it to use this:

Do While Not myProcess.HasExited
Application.DoEvents
Loop

Not the nicest way but it'll do.

I found it funny that VB.NET doesn't solved that behavior that was in VB6 also.

Using APIs this is what I did with VB6 to avoid form freeze and not taking too much resource.

Public Function RunCommand( _
ByRef r_sCommand As String, _
Optional ByRef r_lWindowStyle As Long = API_SW_SHOWNORMAL) As Long

Dim udtSA As SECURITY_ATTRIBUTES_API
Dim udtSI As STARTUPINFO_API
Dim udtPI As PROCESS_INFORMATION_API
Dim lExitCode As Long

RunCommand = -1234567891
udtSA.nLength = Len(udtSA)
udtSI.cb = Len(udtSI)
udtSI.dwFlags = API_STARTF_USESHOWWINDOW
udtSI.wShowWindow = r_lWindowStyle

If ApiCreateProcess(vbNullString$, BetweenQuotes(r_sCommand), udtSA, udtSA, _
API_TRUE&, API_NULL&, ByVal API_NULL&, vbNullString$, udtSI, udtPI) Then

While ApiWaitForSingleObject(udtPI.hProcess&, 100&) = API_WAIT_TIMEOUT
DoEvents
Wend

If ApiGetExitCodeProcess(udtPI.hProcess&, lExitCode&) Then
RunCommand = lExitCode
Else
RunCommand = -1234567890
End If

ApiCloseHandle udtPI.hThread&
ApiCloseHandle udtPI.hProcess&

End If

End Function

Hoping it can be translate to VB.NET. :}

Edited by jdoe
Link to comment
Share on other sites

Seems the easiest way it to use this:

Do While Not myProcess.HasExited
Application.DoEvents
Loop

Not the nicest way but it'll do.

when i read the first post that's exactly what i was going to post.

it could probably benefit from a slight delay thrown in there TBH

welcome to vb, lol.

Link to comment
Share on other sites

it could probably benefit from a slight delay thrown in there TBH
At least 250ms... otherwise it'll stall the CPU and the other processes won't get as much processor time.

I don't know if VB supports this but the best way would be to suspend your process and wait for a notification message when the second process has ended.

Link to comment
Share on other sites

Ok, this seemed to work perfect for me:

procNlite.StartInfo.FileName = txtNlite.Text
procNlite.StartInfo.WindowStyle = ProcessWindowStyle.Normal
tspbMain.Style = ProgressBarStyle.Marquee
Application.DoEvents()
procNlite.Start()
Do Until procNlite.HasExited
Application.DoEvents()
System.Threading.Thread.Sleep(250)
Loop

At first I didn't have the Application.DoEvents in the Do loop and the interface wouldn't repaint and my progress bar wasn't working. But this seems to do the trick for me. My application reports 0% CPU in Task Manager yet the progress bar works and the interface repaints.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...