Home All Groups Group Topic Archive Search About

Help! Monitoring Process with Different Threads Error

Author
13 Jun 2006 8:08 PM
Paul
Program A.EXE starts a new Diagnostics.Process to run program B.EXE.  I
have the EnableRaisingEvents set to True.  When program B.EXE exits the
Exited event fires off, just like I want it to.  I can display a
message box stating that B.EXE has exited.  I have searched the
Internet and have seen numerous examples of that.

However, what I want to do is enable the button on program A.EXE that
started the B.EXE program process.  Below is the code:

Private Sub newProcess_Exited(ByVal sender As Object, ByVal e As
System.EventArgs) Handles newProcess.Exited
    'MessageBox.Show("newProcess_Exited")
    lblOpenApplication1.Enabled = True
    btnOpenApplication1.Enabled = True
End Sub

But I get an "InvalidOperationException" on the
lblOpenApplication1.Enabled = True statement.  The error says,
"Cross-thread operation not valid: Control 'lblOpenApplication1'
accessed from a thread other than the thread it was created on."

AAARRRRGGG!  Can anyone help me?

I know that I can use a timer to monitor the state of the newProcess.
But I would like to get this to work instead.

Author
14 Jun 2006 6:45 AM
Cor Ligthert [MVP]
Paul,

I don't know if it is working, but can you not try the waitforexit?

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.waitforexit.aspx

I hope this helps,

Cor

Show quoteHide quote
"Paul" <pwh***@hotmail.com> schreef in bericht
news:1150229300.203466.255980@y43g2000cwc.googlegroups.com...
> Program A.EXE starts a new Diagnostics.Process to run program B.EXE.  I
> have the EnableRaisingEvents set to True.  When program B.EXE exits the
> Exited event fires off, just like I want it to.  I can display a
> message box stating that B.EXE has exited.  I have searched the
> Internet and have seen numerous examples of that.
>
> However, what I want to do is enable the button on program A.EXE that
> started the B.EXE program process.  Below is the code:
>
> Private Sub newProcess_Exited(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles newProcess.Exited
>    'MessageBox.Show("newProcess_Exited")
>    lblOpenApplication1.Enabled = True
>    btnOpenApplication1.Enabled = True
> End Sub
>
> But I get an "InvalidOperationException" on the
> lblOpenApplication1.Enabled = True statement.  The error says,
> "Cross-thread operation not valid: Control 'lblOpenApplication1'
> accessed from a thread other than the thread it was created on."
>
> AAARRRRGGG!  Can anyone help me?
>
> I know that I can use a timer to monitor the state of the newProcess.
> But I would like to get this to work instead.
>
Author
14 Jun 2006 2:11 PM
Paul
Thanks for responding Cor!

Application A cannot use the WaitForExit.  It is used to open other
applications (C, D, etc.).  Application A is a menu from which users
can run a host of programs at any time.  It must monitor the programs
that it opens and not allow the user to open B, C, etc. more than one
time.

Anyway, I did find what I needed.  When the process application A
started has exited, it fires off the "openApplicationProcess_Exited"
sub (in my app).  Since that is from a different thread, it "Invokes" a
call to the base thread to handle the button enabling and disabling.
Below is the code...

    Private Delegate Sub openApplicationHasExited(ByVal processID As
Integer)
                              .
                              .
                              .
    Private Sub openApplicationHasExitedHandler(ByVal processID As
Integer)
        Try
            Dim applicationTitle As String = ""

            Select Case processID
                              .
                              . (enable/disable menu buttons)
                              .
            End Select
    End Sub
                              .
                              .
                              .

    Private Sub openApplicationProcess_Exited(ByVal sender As Object,
ByVal e As System.EventArgs) Handles openApplicationProcess1.Exited
        Dim myProcessID As Integer = DirectCast(sender, Process).Id
        Dim oahe As New openApplicationHasExited(AddressOf
openApplicationHasExitedHandler)
        Me.Invoke(oahe, New Object() {myProcessID})
    End Sub