Running Apps and Threads

P

Paul

I have a form that functions as a menu. It starts various applications
and keeps track of those apps on whether they are open or not. I have
used both the Shell and Diagnostics.Process.Start procedures to start
the applications. Everything works fine. When an app is started, a
Process variable is used to keep track of it. I define it like this:

Private WithEvents Process1 As Process

When the process starts I make visible a button and label on the menu
form. This button is for the user to click and it will maximize the
associated application that was started. Because of the events that
Process1 raises I want to do the following:

Private Sub Process1_Exited(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Process1.Exited
lblOpenApplication1.Visible = False
btnOpenApplication1.Visible = False
End Sub

It is supposed to make the button and label associated with that app
not visible any more. But I get a "Cross-thread operation not valid"
error when the code hits the first line in the Sub above. I can
understand why. It is because Process1 is created in a different
thread than the menu form. But I don't know how to get around this
using Process1.

I realize that I could have a timer to watch for the process and see if
it still exists. But I would like to avoid that if possible. Any
thoughts?
 
T

Tom Shelton

Paul said:
I have a form that functions as a menu. It starts various applications
and keeps track of those apps on whether they are open or not. I have
used both the Shell and Diagnostics.Process.Start procedures to start
the applications. Everything works fine. When an app is started, a
Process variable is used to keep track of it. I define it like this:

Private WithEvents Process1 As Process

When the process starts I make visible a button and label on the menu
form. This button is for the user to click and it will maximize the
associated application that was started. Because of the events that
Process1 raises I want to do the following:

Private Sub Process1_Exited(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Process1.Exited
lblOpenApplication1.Visible = False
btnOpenApplication1.Visible = False
End Sub

You need to marshal the call back to the forms thread... Something
like:

' assuming your handling this with in the form...
Private Sub Process1_Exited (ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Process1.Exited

If Me.InvokeRequired Then
Me.Invoke (New EventHandler (Address Of Me.Process_Exited), _
New Object() {sender, e})
else
lblOpenApplication1.Visible = False
btnOpenApplication1.Visible = False
endif
end sub

Here's a multipart article on the subject over on msdn:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms08162002.asp

that is part 1 of a 3 part article. The code in the article is C#, but
the concept is the same...
 

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