Running Apps and Threads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    #1

    Running Apps and Threads

    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.Pro cess.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.EventArg s) Handles Process1.Exited
    lblOpenApplicat ion1.Visible = False
    btnOpenApplicat ion1.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?

  • Tom Shelton

    #2
    Re: Running Apps and Threads


    Paul wrote:[color=blue]
    > 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.Pro cess.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.EventArg s) Handles Process1.Exited
    > lblOpenApplicat ion1.Visible = False
    > btnOpenApplicat ion1.Visible = False
    > End Sub[/color]

    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.EventArg s) Handles Process1.Exited

    If Me.InvokeRequir ed Then
    Me.Invoke (New EventHandler (Address Of Me.Process_Exit ed), _
    New Object() {sender, e})
    else
    lblOpenApplicat ion1.Visible = False
    btnOpenApplicat ion1.Visible = False
    endif
    end sub

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

    http://msdn.microsoft.com/library/de...ms08162002.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]

    Comment

    Working...