Waiting of process completion

  • Thread starter Raymond Lewallen
  • Start date
R

Raymond Lewallen

How to wait for a process to stop completion is my goal. Obviously, the
looping while waiting for the HasExited property is not a solution.. but
thats the best I can come up off the top of my head. Natuarally it will not
work. I expect it to use up all resources looping, which will not allow the
process to complete. The process takes about 60 seconds, because the .bat
file it is calling is rebuilding mulitple .NET solutions and projects and
copying the assembly files out to other directories.

So in short, I'm looking for an efficient way to wait for the process to
complete before continuting to execute the remainder of the code in the Sub.

I have the following code (only what you need to help me, if you need to see
more, let me know, but the government doesn't like me putting a bunch of
source code out there if it isn't relevant.):

**********************************************
Public Class frmMerge

Private processRebuildAW As Process

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCreate.Click

ClearListBox()

ClearProgressBar()

#If Not Debug Then
IF chkRebuildAW.Checked = True Then RebuildAWPath()

Do While Not (processRebuildAW.HasExited)

' RIGHT HERE IS MY PROBLEM

Loop



#End If

lblReadyCount.Text = Convert.ToString(lstFiles.Items.Count)

blah blah blah more code more code more code

End Sub

Private Sub RebuildAWPath()

Dim strPath As String = Application.StartupPath & "\rebuildawpath.bat"

processRebuildAW = Process.Start(strPath)

End Sub

End Class

**********************************************
 
C

CJ Taylor

have you tried to use a delegate to call your method asyncronously? That
way you can have an AsyncCallBack method to be invoked when your method is
done, so your not polling your process (or at least do it asynchounsly so
you can give control back to the user)

-CJ

FAA hiring?

=)
 
C

Cor

Hi Raymond,

I did not look at your solution before, but now I see it I think it starts a
shell proces, and I understand you want to wait until that is ready.

I think that the solution is very simple place this behind your start of the
batch file. It has to wait for the last standardOutput and therefore to wait
until that program is ready.

I hope that I did understand that problem well?

Cor

\\\
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim sb As New System.Text.StringBuilder("")
Dim input As Integer = sr.Read
Do Until input = -1
sb.Append(ChrW(input))
input = sr.Read
Loop
///
 
R

Raymond Lewallen

CJ,

Please look at the following changes. I have run this and its just keep
right on executing the remainder of the code in the sub without waiting for
the batch file to complete first. The reason I need to batch file to
complete first is because it is rebuilding .NET assemblies that the
remainder of the sub is going to use, therefore they need to be built first
and copied, which is what the .bat file does, and then the remainder of the
code can run.

Thanks for you help, you're helping out with a problem I really don't want
to have to work around.

Now I have the following:

**********************************************
Public Class frmMerge

Private processRebuildAW As Process
Delegate Sub AsyncRebuildAW()

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCreate.Click

ClearListBox()

ClearProgressBar()

#If Not Debug Then

If chkRebuildAW.Checked = True Then

Dim objAsync As New AsyncRebuildAW(AddressOf RebuildAWPath)

Dim objResult As IAsyncResult = objAsync.BeginInvoke(Nothing, Nothing)

Do Until objResult.IsCompleted

Loop

objAsync.EndInvoke(objResult)

End If


#End If

lblReadyCount.Text = Convert.ToString(lstFiles.Items.Count)

blah blah blah more code more code more code

End Sub

Private Sub RebuildAWPath()

Dim strPath As String = Application.StartupPath & "\rebuildawpath.bat"

processRebuildAW = Process.Start(strPath)

End Sub

End Class

**********************************************
 
R

Raymond Lewallen

Cor,

Is the solution below attempting to capture output from the DOS shell? If
so, will this looping put me back into the same situation I'm trying to get
out of, which is the looping is using so much computer resources, that the
DOS shell can't finish whats its trying to do due to lack of resources/CPU
availability?

Thanks for your help. In waiting for your response, I will go ahead and
implement what you have given me below. Taking the time from your day to
help me is greatly appreciated.

Raymond Lewallen
 
R

Raymond Lewallen

CJ,

Regarding the FAA hiring: the best way to attempt to get into the FAA, or
any other government agency as a IT person, regardless of network, software,
security etc.. is to get on as a contractor first. I'll tell you how I got
on... go to http://www.lockheedmartin.com click on Careers-Submit a Resume.
Give them your information, as they are a very, very large provider of IT
professionals to the US Gov't. Took me 6 months before I ever got a call
from LMIT (Lockheed-Martin Information Technology) after submitting my
resume, but the end result and current job was worth waiting the 6 months
for, even if I did have to sell cars in the meantime :) If you do put in
your resume, put me down as a referal. They pay me money for hiring
referals :)

Hope this information helps anyone out there looking to get a foot into the
door of government software development. Its a great opportunity if
available and offered to you, IMO.

Raymond Lewallen
 
C

CJ Taylor

Raymond,

Thanks for the FAA info.. now regarding your problem.

Screw the do until loop, your just wasting time and not ever returning
control back to the user. Doing it that way, you might as well just call
the method directly.

on your async delegate, the first parameter is another delegate to a
callback method. Create another method that has the signature

private sub callbackmethod(ar as IAsyncResult)

Dim asyncR as AsyncResult ' this is in System.runtime.remoting.messaging
namespace

asyncR = ctype(ar, AsyncResult)

dim asyncDel as AsyncRebuildAW

asyncDel = Ctype(asyncR.AsyncDelegate, AsyncrebuildAw)

asyncDel.EndInvoke(ar)

'now process whatever you want from your batch file... this means its
executed

end sub

And thats about all she wrote.. hope it helps.

-CJ
 
R

Raymond Lewallen

Now i have the following, but the code below "NOTHING BELOW IS BEING
EXECUTED" is doing just that, not executing.

Sorry to be such a bother, I'm a DBA working in about 12 different .NET
projects at the moment, and this code is a side project to help me manage
the building of assemblies and merging of projects of the other 12.

Again, there is another 850 lines of code I've excluded, but those are not
relevant to this situation.

Thanks for all you help,

Raymond Lewallen

Public Class frmMerge
Inherits System.Windows.Forms.Form

Private processRebuildAW As Process
Delegate Sub AsyncRebuildAW()

Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCreate.Click
ClearListBox()
ClearProgressBar()
If chkRebuildAW.Checked = True Then
Dim objAsync As New AsyncRebuildAW(AddressOf RebuildAWPath)
Dim objCB As New AsyncCallback(AddressOf callbackmethod)
Dim objAR As IAsyncResult
objAR = objAsync.BeginInvoke(Nothing, objCB)
End If
End Sub

Private Sub RebuildAWPath()
Dim strPath As String = Application.StartupPath &
"\rebuildawpath.bat"
processRebuildAW = Process.Start(strPath)
End Sub

Private Sub callbackmethod(ByVal ar As IAsyncResult)
Dim asyncR As System.runtime.remoting.messaging.AsyncResult ' this
is in System.runtime.remoting.messaging namespace
asyncR = CType(ar, System.runtime.remoting.messaging.AsyncResult)
Dim asyncDel As AsyncRebuildAW
asyncDel = CType(asyncR.AsyncDelegate, AsyncRebuildAW)
asyncDel.EndInvoke(ar)

' *********************************************
' *** NOTHING BELOW IS BEING EXECUTED.
' *********************************************
Dim itemChecked As Object
Dim strCheckedFile As String
Dim strMovedFile As String
For Each itemChecked In clbFiles.CheckedItems
strCheckedFile = itemChecked.ToString()
If InStr(strCheckedFile, txtACRADirectory.Text) > 0 Then
strMovedFile = Replace(strCheckedFile,
txtACRADirectory.Text, txtMergeDirectory.Text)
ElseIf InStr(strCheckedFile, txtAWDirectory.Text) > 0 Then
strMovedFile = Replace(strCheckedFile,
txtAWDirectory.Text, txtMergeDirectory.Text)
Else
strMovedFile = strCheckedFile
End If
lstFiles.Items.Add(strMovedFile)
Next
lblReadyCount.Text = Convert.ToString(lstFiles.Items.Count)
If lstFiles.Items.Count > 0 Then
btnWebsite.Enabled = True
Else
btnWebsite.Enabled = False
End If
lstFiles.Sorted = True
End Sub

End Class
 
C

CJ Taylor

Hmmm... forgot that Process.Start() executes on a separate thread. I take
it that it was jupming pretty quick from the process start to the callback.

Anyways, we need to watch for the exiting. Now we can do this through an
event, but lets not waste any more time, your busy as I can tell.

Anyways, here is where we want to do our looping. just do a simple while
not Process.HasExited
... sleep, I dunno, whatever.

and then when it does exit, your call back will be properly called and you
wont hang on the EndInvoke.

Try it out and let me know.

-CJ
 
R

Raymond Lewallen

CJ,

Thanks for all your help. I took out all the delegate stuff and just
inserted:
do while not (processRebuildAW.HasExited)
System.Threading.Thread.Sleep(1500)
loop
After my call to the sub that launches process.start and that seems to be
working fine now. Thought there might be a cleaner way to do this, but at
this point I just wanted it to work :)

You've been a great help. Sorry for not understanding better, like I said,
I'm an MCDBA but have been doing .NET coding for about 8 months now and
don't understand as much as I would like about more complex .NET scenarios;
complex to me, anyways :) .

Thanks for helping me get going on this. I believe I can release it to the
senior developers now. That was the only hitch I had left to solve.
When/If you have time/get bored I would like to know how to handle this
through an event. I may work on it this weekend. We have a code freeze on
the 7th of April and a rollout on the 30th of April, so I'll have some time
to fine tune this between those dates as well while QA does its part.

Raymond Lewallen
 
C

Cor

Hi Raymond,

I do not understand this about resources, what happens in my idea is that
the process start and the program nicely waits for the end that is sended to
the console. I did copied this from a program, but even that stringbuilder
that was in is unnecessary if you are sure that there is no output to the
console.
\\\
p.Start()
Dim sr As IO.StreamReader = p.StandardOutput
Dim input As Integer = sr.Read
Do Until input = -1
input = sr.Read
Loop
///
Cor
 
C

CJ Taylor

Raymond,

Hey, as long as it works right? By doing that, you don't have true async
stuff going on, but it will work for now. its a simple fix.. This should
be all you need

Private Sub RebuildAWPath()
Dim strPath As String = Application.StartupPath &
"\rebuildawpath.bat"
processRebuildAW = Process.Start(strPath)

'''-----------------inserted

do while not (processRebuildAW.HasExited)
System.Threading.Thread.Sleep(1500)
loop


End Sub


this will keep that background thread that is actually monitoring the
process alive and prevent the callback method from being invoked.

then when .HasExited is true, the method is invoked. which you could do
your completed processing after that.

As for being a help, not a problem. I'm pretty impressed that a DBA has
this much .NET knowledge. No offesne or anything, but a lot of dba's I meet
just want to do there sql/oracle and call it a day. (a few postgres guys in
there too.)

I've been doing .NET for about 2 years now, and I still learn new stuff
every day from it, maybe not in the language constructs themselves, but in
how to handle situations. These groups are great for it.

Anyways, thanks for the advice on faa, and if you need to contact me at
home, please feel free with questions.

my address is cege at tavayn dot no spam com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top