multithread method invoke

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

    #1

    multithread method invoke

    Hello

    does someone know how i can invoke a method in the underlying thread without
    the usage of a window handle ??


    This works perfect in a form

    Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))



    however in a control i receive the folowing error

    "Cannot call Invoke or InvokeAsync on a control until the window handle has
    been created."







    the complete routine that runs on a background thread:

    Private Sub Listener()

    done = False

    Try

    While Not done

    Dim iPEndPoint As IPEndPoint = Nothing

    Dim bs As Byte() = client.Receive( iPEndPoint)

    Dim m As IMessage = NHS.Messaging.U til.RetrieveMes sage(bs)

    If Not m Is Nothing Then 'Return

    message = m.MessageText

    ' need to use Invoke or BeginInvoke Syntax as message is on different
    thread--

    Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))

    End If

    End While

    Catch e As Exception

    multicastExcept ion(e)

    End Try

    End Sub


  • rawCoder

    #2
    Re: multithread method invoke

    Just a trick, see if it works,

    try doing dim nHandle as integer = Me.Handle
    before the invoke line

    HTH
    rawCoder

    "m.posseth" <michelp@nohaus ystems.nl> wrote in message
    news:429de7f7$0 $17462$ba620dc5 @nova.planet.nl ...[color=blue]
    > Hello
    >
    > does someone know how i can invoke a method in the underlying thread[/color]
    without[color=blue]
    > the usage of a window handle ??
    >
    >
    > This works perfect in a form
    >
    > Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))
    >
    >
    >
    > however in a control i receive the folowing error
    >
    > "Cannot call Invoke or InvokeAsync on a control until the window handle[/color]
    has[color=blue]
    > been created."
    >
    >
    >
    >
    >
    >
    >
    > the complete routine that runs on a background thread:
    >
    > Private Sub Listener()
    >
    > done = False
    >
    > Try
    >
    > While Not done
    >
    > Dim iPEndPoint As IPEndPoint = Nothing
    >
    > Dim bs As Byte() = client.Receive( iPEndPoint)
    >
    > Dim m As IMessage = NHS.Messaging.U til.RetrieveMes sage(bs)
    >
    > If Not m Is Nothing Then 'Return
    >
    > message = m.MessageText
    >
    > ' need to use Invoke or BeginInvoke Syntax as message is on different
    > thread--
    >
    > Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))
    >
    > End If
    >
    > End While
    >
    > Catch e As Exception
    >
    > multicastExcept ion(e)
    >
    > End Try
    >
    > End Sub
    >
    >[/color]


    Comment

    • m.posseth

      #3
      Re: multithread method invoke

      Well ,,,, after a slight conversion :-)
      Dim nHandle As IntPtr = Me.Handle

      This seems to work ( the error isn`t raised annymore , now see if it is
      receiving data )

      The problem is solved however i am still curious why it occured :-)

      Private Sub Listener()

      done = False

      Try

      While Not done

      Dim iPEndPoint As IPEndPoint = Nothing

      Dim bs As Byte() = client.Receive( iPEndPoint)

      Dim m As IMessage = NHS.Messaging.U til.RetrieveMes sage(bs)

      If Not m Is Nothing Then Return

      message = m.MessageText

      Dim nHandle As IntPtr = Me.Handle

      ' need to use Invoke or BeginInvoke Syntax as message is on different
      thread--

      Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))

      End While

      Catch e As Exception

      multicastExcept ion(e)

      End Try

      End Sub

      Thanks for helping me out

      Regards

      Michel Posseth






      "rawCoder" <rawCoder@hotma il.com> wrote in message
      news:%23hrXDLtZ FHA.3676@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Just a trick, see if it works,
      >
      > try doing dim nHandle as integer = Me.Handle
      > before the invoke line
      >
      > HTH
      > rawCoder
      >
      > "m.posseth" <michelp@nohaus ystems.nl> wrote in message
      > news:429de7f7$0 $17462$ba620dc5 @nova.planet.nl ...[color=green]
      >> Hello
      >>
      >> does someone know how i can invoke a method in the underlying thread[/color]
      > without[color=green]
      >> the usage of a window handle ??
      >>
      >>
      >> This works perfect in a form
      >>
      >> Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))
      >>
      >>
      >>
      >> however in a control i receive the folowing error
      >>
      >> "Cannot call Invoke or InvokeAsync on a control until the window handle[/color]
      > has[color=green]
      >> been created."
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >> the complete routine that runs on a background thread:
      >>
      >> Private Sub Listener()
      >>
      >> done = False
      >>
      >> Try
      >>
      >> While Not done
      >>
      >> Dim iPEndPoint As IPEndPoint = Nothing
      >>
      >> Dim bs As Byte() = client.Receive( iPEndPoint)
      >>
      >> Dim m As IMessage = NHS.Messaging.U til.RetrieveMes sage(bs)
      >>
      >> If Not m Is Nothing Then 'Return
      >>
      >> message = m.MessageText
      >>
      >> ' need to use Invoke or BeginInvoke Syntax as message is on different
      >> thread--
      >>
      >> Me.Invoke(New MethodInvoker(A ddressOf ShowRecvdMessag e))
      >>
      >> End If
      >>
      >> End While
      >>
      >> Catch e As Exception
      >>
      >> multicastExcept ion(e)
      >>
      >> End Try
      >>
      >> End Sub
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • rawCoder

        #4
        Re: multithread method invoke

        I read this solution some times ago on the newsgroup and was in the back of
        my mind, but I forgot the datatype catch.
        Well I think someone said that this line forces the handle to be created.
        If this is THE window handle, then its very strange.
        Some one has recommended to call the CreateHandle/CreateControl method as
        well.

        HTH
        rawCoder


        Comment

        Working...