Home All Groups Group Topic Archive Search About

Running Apps and Threads

Author
14 Apr 2006 5:03 PM
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?

Author
14 Apr 2006 6:21 PM
Tom Shelton
Paul wrote:
Show quoteHide quote
> 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...

--
Tom Shelton [MVP]