Cross Thread Operation not valid.

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

    #1

    Cross Thread Operation not valid.

    Hi,
    Moving a project from .net 2003 -> 2005 Beta 2
    Windows App. Main Window is start object.
    Main window spawns a thread.
    After doing some work this thread raises an interrupt.
    The event carries a reference to the class that raised it plus an
    instantiated, empty eventarg.
    RaiseEvent LineStateChange (Me, e)

    The 'RaiseEvent' errors in 2005 with Cross thread...

    Any ideas on what is going on here?
    Thanks
    Bob


  • TrtnJohn

    #2
    RE: Cross Thread Operation not valid.

    You should check if InvokeRequired is set on the ISynchronizeInv oke
    interfaced of your event sink before raising the event from a separate
    thread. If this event sink requires synchronization , you'll need to call
    Invoke instead. Here is some sample code of a OnTestEvent that implements
    this.

    Public Event TestEvent as EventHandler

    Protected Overridable Sub OnTestEvent(ByV al sender As Object, ByVal args
    As System.EventArg s)
    If Not TestEventEvent Is Nothing Then
    Dim singlecast As EventHandler

    'Check each multi-cast delegate
    For Each singlecast In TestEventEvent. GetInvocationLi st
    Dim syncInvoke As System.Componen tModel.ISynchro nizeInvoke
    If TypeOf singlecast.Targ et Is
    System.Componen tModel.ISynchro nizeInvoke Then
    syncInvoke = CType(singlecas t.Target,
    System.Componen tModel.ISynchro nizeInvoke)
    End If
    If Not syncInvoke Is Nothing Then
    If syncInvoke.Invo keRequired Then
    syncInvoke.Invo ke(singlecast, New Object() {Me, New
    EventArgs})
    Else
    singlecast(Me, Nothing)
    End If
    End If
    Next


    End If


    End Sub

    "Bob" wrote:
    [color=blue]
    > Hi,
    > Moving a project from .net 2003 -> 2005 Beta 2
    > Windows App. Main Window is start object.
    > Main window spawns a thread.
    > After doing some work this thread raises an interrupt.
    > The event carries a reference to the class that raised it plus an
    > instantiated, empty eventarg.
    > RaiseEvent LineStateChange (Me, e)
    >
    > The 'RaiseEvent' errors in 2005 with Cross thread...
    >
    > Any ideas on what is going on here?
    > Thanks
    > Bob
    >
    >
    >[/color]

    Comment

    • Bob

      #3
      Re: Cross Thread Operation not valid.

      Hi,
      Thanks for the snippet.
      Based on this and some other sample code I ended up hatching a delegate in
      the main window and invoking the delegate in the worker thread.
      regards
      Bob
      "TrtnJohn" <TrtnJohn@discu ssions.microsof t.com> wrote in message
      news:3689958C-4515-42EA-B233-9BC7E552EC70@mi crosoft.com...[color=blue]
      > You should check if InvokeRequired is set on the ISynchronizeInv oke
      > interfaced of your event sink before raising the event from a separate
      > thread. If this event sink requires synchronization , you'll need to call
      > Invoke instead. Here is some sample code of a OnTestEvent that implements
      > this.
      >
      > Public Event TestEvent as EventHandler
      >
      > Protected Overridable Sub OnTestEvent(ByV al sender As Object, ByVal[/color]
      args[color=blue]
      > As System.EventArg s)
      > If Not TestEventEvent Is Nothing Then
      > Dim singlecast As EventHandler
      >
      > 'Check each multi-cast delegate
      > For Each singlecast In TestEventEvent. GetInvocationLi st
      > Dim syncInvoke As System.Componen tModel.ISynchro nizeInvoke
      > If TypeOf singlecast.Targ et Is
      > System.Componen tModel.ISynchro nizeInvoke Then
      > syncInvoke = CType(singlecas t.Target,
      > System.Componen tModel.ISynchro nizeInvoke)
      > End If
      > If Not syncInvoke Is Nothing Then
      > If syncInvoke.Invo keRequired Then
      > syncInvoke.Invo ke(singlecast, New Object() {Me,[/color]
      New[color=blue]
      > EventArgs})
      > Else
      > singlecast(Me, Nothing)
      > End If
      > End If
      > Next
      >
      >
      > End If
      >
      >
      > End Sub
      >
      > "Bob" wrote:
      >[color=green]
      > > Hi,
      > > Moving a project from .net 2003 -> 2005 Beta 2
      > > Windows App. Main Window is start object.
      > > Main window spawns a thread.
      > > After doing some work this thread raises an interrupt.
      > > The event carries a reference to the class that raised it plus an
      > > instantiated, empty eventarg.
      > > RaiseEvent LineStateChange (Me, e)
      > >
      > > The 'RaiseEvent' errors in 2005 with Cross thread...
      > >
      > > Any ideas on what is going on here?
      > > Thanks
      > > Bob
      > >
      > >
      > >[/color][/color]


      Comment

      • Bob

        #4
        Re: Cross Thread Operation not valid.

        Hi,
        As stated further down in this thread. (Oh No , that word again.) I have a
        solution that involves the working thread invoking a delegate in the calling
        thread (main Window.)
        However this troubles me.
        There seems to be too much coupling IMHO.
        In .net 2003, rightly or wrongly, I could design my worker class without
        prior knowledge of the calling class. It could be designed to do its work
        then raise its Publicly declared event with its payload.
        The event handler on the main window could be designed later.
        Now it seems I have to know about the main form's delegate before I can
        write the beginInvoke instruction in the worker class.
        I hope I have got it wrong and someone can enlighten me. Otherwise this
        seems a large leap backwards.
        regards
        Bob
        "Bob" <bob@nowhere.co m> wrote in message
        news:O4uEpJN2FH A.476@TK2MSFTNG P15.phx.gbl...[color=blue]
        > Hi,
        > Moving a project from .net 2003 -> 2005 Beta 2
        > Windows App. Main Window is start object.
        > Main window spawns a thread.
        > After doing some work this thread raises an interrupt.
        > The event carries a reference to the class that raised it plus an
        > instantiated, empty eventarg.
        > RaiseEvent LineStateChange (Me, e)
        >
        > The 'RaiseEvent' errors in 2005 with Cross thread...
        >
        > Any ideas on what is going on here?
        > Thanks
        > Bob
        >
        >[/color]


        Comment

        • Bob

          #5
          Re: Cross Thread Operation not valid.

          Hi,
          Please ignore this bleat.
          I have actually read the documenation and found the correct 'uncoupled' way
          of doing events.
          Thanks
          Bob
          "Bob" <bob@nowhere.co m> wrote in message
          news:ee7sINP2FH A.1140@tk2msftn gp13.phx.gbl...[color=blue]
          > Hi,
          > As stated further down in this thread. (Oh No , that word again.) I have a
          > solution that involves the working thread invoking a delegate in the[/color]
          calling[color=blue]
          > thread (main Window.)
          > However this troubles me.
          > There seems to be too much coupling IMHO.
          > In .net 2003, rightly or wrongly, I could design my worker class without
          > prior knowledge of the calling class. It could be designed to do its work
          > then raise its Publicly declared event with its payload.
          > The event handler on the main window could be designed later.
          > Now it seems I have to know about the main form's delegate before I can
          > write the beginInvoke instruction in the worker class.
          > I hope I have got it wrong and someone can enlighten me. Otherwise this
          > seems a large leap backwards.
          > regards
          > Bob
          > "Bob" <bob@nowhere.co m> wrote in message
          > news:O4uEpJN2FH A.476@TK2MSFTNG P15.phx.gbl...[color=green]
          > > Hi,
          > > Moving a project from .net 2003 -> 2005 Beta 2
          > > Windows App. Main Window is start object.
          > > Main window spawns a thread.
          > > After doing some work this thread raises an interrupt.
          > > The event carries a reference to the class that raised it plus an
          > > instantiated, empty eventarg.
          > > RaiseEvent LineStateChange (Me, e)
          > >
          > > The 'RaiseEvent' errors in 2005 with Cross thread...
          > >
          > > Any ideas on what is going on here?
          > > Thanks
          > > Bob
          > >
          > >[/color]
          >
          >[/color]


          Comment

          Working...