Results 1 to 8 of 8

Thread: Exiting from a Thread/Process

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    8

    Exiting from a Thread/Process

    I have a code from this website, that I am trying to work with.
    Basically, I am just trying to close the thread/process after it's done.

    Code:
        Private psi As ProcessStartInfo
        Private cmd As Process
        Public Delegate Sub InvokeWithString(ByVal text As String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim psi As New ProcessStartInfo("m7.bat")
    
            Dim systemencoding As System.Text.Encoding = _
                System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
    
            With psi
                .UseShellExecute = False
                .RedirectStandardError = True
                .RedirectStandardOutput = True
                .RedirectStandardInput = True
                .CreateNoWindow = True
                .StandardOutputEncoding = systemencoding
                .StandardErrorEncoding = systemencoding
            End With
    
            ' EnableraisingEvents is required for Exited event
            cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
    
            AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
            AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
            AddHandler cmd.Exited, AddressOf CMD_Exited
    
            cmd.Start()
            cmd.BeginOutputReadLine()
            cmd.BeginErrorReadLine()
    
        End Sub
    The problem is in this code:
    Code:
     Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
            Me.Invoke(New InvokeWithString(AddressOf CloseME), e)
            MsgBox("Done")
            'Me.Close()
        End Sub
    I am trying to close the thread so once it's done, it closed for good and my form is closed as well. I tried Me.Close() which results in: "Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on." Error.
    The code: Me.Invoke(New InvokeWithString(AddressOf CloseME), e) - Results in parameter mismatch error.

    However, things like Msgbox("Done") works perfectly.

    I am trying to understand this, I am new to VB and I am self-teaching my self everything so it's kind of hard.

    Thank you everyone

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Exiting from a Thread/Process

    This happens because you initiated 2 async methods here:
    Code:
    cmd.BeginOutputReadLine()
    cmd.BeginErrorReadLine()
    but forgot to cancel them using
    Code:
    cmd.CancelOutputRead()
    cmd.CancelErrorRead()
    Taken from here (http://msdn.microsoft.com/en-us/libr...treadline.aspx )

    You can cancel an asynchronous read operation by calling CancelOutputRead. The read operation can be canceled by the caller or by the event handler. After canceling, you can call BeginOutputReadLine again to resume asynchronous read operations.

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    8

    Re: Exiting from a Thread/Process

    Thank you for your reply,

    I've attached the source code I am working with, in this post.

    I first entered cmd.CancelOutputRead(), cmd.CancelErrorRead() on CMD_Exit sub but realized that doesn't make sense.

    I tried to do something like this:
    Code:
    cmd.Start()
    cmd.BeginOutputReadLine()
    cmd.BeginErrorReadLine()
    
    if cmd."done reading lines" then
           cmd.CancelOutputRead()
           cmd.CancelErrorRead()
    end if
    I am a little confused on this, where would I add the cmd.CancelOutputRead() ?

    I added this code: Control.CheckForIllegalCrossThreadCalls = False on form load, and that fixed the problem, however this is not a good method of doing so, therefore I would not like to use this method.

    Any help is greatly appreciated, thank you
    Attached Files Attached Files

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    8

    Re: Exiting from a Thread/Process

    That is exactly where I put my cancellation commands.
    inside Sub CMD_Exited, but my errors still remain.

    Me.close, error: Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.

    Code:
    Code:
        Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
            cmd.CancelOutputRead()
            cmd.CancelErrorRead()
            MsgBox("Done")
            Me.Close()
        End Sub

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Exiting from a Thread/Process

    Try this:

    Code:
    Private Delegate Sub NoParamsDelegate()
    
    Private Sub CloseForm()
        Me.Close()
    End Sub
    
    Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
        cmd.CancelOutputRead()
        cmd.CancelErrorRead()
        Me.Invoke(New NoParamsDelegate(AddressOf CloseForm))
    End Sub

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2012
    Posts
    8

    Re: Exiting from a Thread/Process

    That worked perfectly, thank you so much.

    I was trying something like
    Code:
    Me.Invoke(New InvokeWithString(AddressOf CloseME))
    I am not sure why that didn't work. I guess I just needed to create a new delegate sub and assign it to close the form ?

    I am still a little confused but it works now, so I am happy

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Exiting from a Thread/Process

    Delegate signature must match the signature of the method being called.
    That InvokeWithString was supposed to invoke a method that accepts a string. Its signature looked like Sub(String) whileas Sub CloseMe() is called without any.
    A method signature is a list of its arguments, their type and the return type of the method itself, i.e. the signature of the standard Form's event handler is Sub(Object, EventArgs), the signature of CloseMe was simply Sub()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width