Threading

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

    #1

    Threading

    Can anyone recommend a good [web-based] reference on Threading
    (Framework 1.1)?

    I'm particularly confused about how to [safely] call a method in my
    "main" thread from another one. - or more, one day ;-)

    I've tried creating delegates to main thread methods and Invoking them
    and BeginInvoking them, but I've had some quite /horrendous/ results ...

    TIA,
    Phill W.
  • AlanT

    #2
    Re: Threading


    Phill W. wrote:[color=blue]
    > Can anyone recommend a good [web-based] reference on Threading
    > (Framework 1.1)?
    >
    > I'm particularly confused about how to [safely] call a method in my
    > "main" thread from another one. - or more, one day ;-)
    >
    > I've tried creating delegates to main thread methods and Invoking them
    > and BeginInvoking them, but I've had some quite /horrendous/ results ...
    >
    > TIA,
    > Phill W.[/color]

    You might try Ted Pattison's articles on threading/delegates et al. in
    MSDN magazine

    http://msdn.microsoft.com/msdnmag/fi...on&words=exact


    hth,
    Alan

    Comment

    • RMT

      #3
      Re: Threading


      Yes, invoking or begininvoking is the way to go - do you have any code?
      seems like you are almost there.......... .........



      "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> escribió en el mensaje
      news:e6bp4o$89l $1@yarrow.open. ac.uk...[color=blue]
      > Can anyone recommend a good [web-based] reference on Threading (Framework
      > 1.1)?
      >
      > I'm particularly confused about how to [safely] call a method in my "main"
      > thread from another one. - or more, one day ;-)
      >
      > I've tried creating delegates to main thread methods and Invoking them and
      > BeginInvoking them, but I've had some quite /horrendous/ results ...
      >
      > TIA,
      > Phill W.[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Threading

        Phill,

        In the dotnet newsgroups there is only one constant reference



        I hope this helps,

        Cor

        "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> schreef in bericht
        news:e6bp4o$89l $1@yarrow.open. ac.uk...[color=blue]
        > Can anyone recommend a good [web-based] reference on Threading (Framework
        > 1.1)?
        >
        > I'm particularly confused about how to [safely] call a method in my "main"
        > thread from another one. - or more, one day ;-)
        >
        > I've tried creating delegates to main thread methods and Invoking them and
        > BeginInvoking them, but I've had some quite /horrendous/ results ...
        >
        > TIA,
        > Phill W.[/color]


        Comment

        • Brian Gideon

          #5
          Re: Threading

          Phill,

          Jon's article that Cor posted is an excellent reference.
          Unfortunately, the examples are in C#. That's typical of articles
          related to threading. The better ones seem to use C# examples. With
          that said here's another good article.

          <http://www.albahari.co m/threading/>

          Brian

          Phill W. wrote:[color=blue]
          > Can anyone recommend a good [web-based] reference on Threading
          > (Framework 1.1)?
          >
          > I'm particularly confused about how to [safely] call a method in my
          > "main" thread from another one. - or more, one day ;-)
          >
          > I've tried creating delegates to main thread methods and Invoking them
          > and BeginInvoking them, but I've had some quite /horrendous/ results ...
          >
          > TIA,
          > Phill W.[/color]

          Comment

          • Phill W.

            #6
            Re: Threading


            Yep; and I've been "almost there" for a while now.

            I started to worry when I put this threading code in and suddenly
            started getting apparently random Exceptions all over my application
            each with 15-20 lines of Stack Trace in them, /every one/ mentioning
            System.Windows.-something-or-other and not a mention of /my/ code
            anywhere in sight... 8-}

            Here's my "thread" class :

            Private Class SideStep

            Friend Sub New(ByVal oCB As Rejoin, ByVal iDelay As Integer)
            m_oCallbackDele gate = oCB
            m_iDelay = iDelay
            End Sub

            Public Delegate Sub Rejoin()

            Public Sub Skip()
            Dim t As New Threading.Threa d(AddressOf Me.Run)
            t.Name = "WM_Activat eApp - SideStep"
            t.Start()
            End Sub

            Private Sub Run()
            System.Threadin g.Thread.Sleep( m_iDelay)
            m_oCallbackDele gate.Invoke()
            End Sub

            Private m_oCallbackDele gate As Rejoin = Nothing
            Private m_iDelay As Integer = 10

            End Class

            and this is how I use it

            Protected Overrides Sub WndProc(ByRef m As Message)

            Const WM_ACTIVATEAPP As Integer = &H1C

            Select Case m.Msg
            Case WM_ACTIVATEAPP
            If m.WParam.ToInt3 2 = 1 Then
            Dim d As New SideStep.Rejoin (AddressOf Me.OnAppActivat e)
            Dim ss As New SideStep(d)
            ss.Skip()
            End If
            . . .
            End Select

            MyBase.WndProc( m)

            End Sub

            Regards,
            Phill W.

            RMT wrote:[color=blue]
            > Yes, invoking or begininvoking is the way to go - do you have any code?
            > seems like you are almost there.......... .........
            >
            >
            >
            > "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> escribió en el mensaje
            > news:e6bp4o$89l $1@yarrow.open. ac.uk...[color=green]
            >> Can anyone recommend a good [web-based] reference on Threading (Framework
            >> 1.1)?
            >>
            >> I'm particularly confused about how to [safely] call a method in my "main"
            >> thread from another one. - or more, one day ;-)
            >>
            >> I've tried creating delegates to main thread methods and Invoking them and
            >> BeginInvoking them, but I've had some quite /horrendous/ results ...
            >>
            >> TIA,
            >> Phill W.[/color]
            >
            >[/color]

            Comment

            • Brian Gideon

              #7
              Re: Threading

              Phill,

              The problem is that the m_oCallbackDele gate delegate is executed on the
              worker thread. If OnAppActivate is touching UI forms or controls then
              you're going to have problems. What you need to do is marshal the
              execution of m_oCallbackDele gate onto the UI thread. This can be done
              by first getting a reference to one of the applications Form classes
              into the SideStep class. Then instead of making the call
              m_oCallbackDele gate.Invoke pass the delegate as a parameter into
              yourForm.BeginI nvoke. The end result is that OnAppActivate will be
              execute on the UI thread where you can safely access forms and
              controls.

              Brian

              Phill W. wrote:[color=blue]
              > Yep; and I've been "almost there" for a while now.
              >
              > I started to worry when I put this threading code in and suddenly
              > started getting apparently random Exceptions all over my application
              > each with 15-20 lines of Stack Trace in them, /every one/ mentioning
              > System.Windows.-something-or-other and not a mention of /my/ code
              > anywhere in sight... 8-}
              >
              > Here's my "thread" class :
              >
              > Private Class SideStep
              >
              > Friend Sub New(ByVal oCB As Rejoin, ByVal iDelay As Integer)
              > m_oCallbackDele gate = oCB
              > m_iDelay = iDelay
              > End Sub
              >
              > Public Delegate Sub Rejoin()
              >
              > Public Sub Skip()
              > Dim t As New Threading.Threa d(AddressOf Me.Run)
              > t.Name = "WM_Activat eApp - SideStep"
              > t.Start()
              > End Sub
              >
              > Private Sub Run()
              > System.Threadin g.Thread.Sleep( m_iDelay)
              > m_oCallbackDele gate.Invoke()
              > End Sub
              >
              > Private m_oCallbackDele gate As Rejoin = Nothing
              > Private m_iDelay As Integer = 10
              >
              > End Class
              >
              > and this is how I use it
              >
              > Protected Overrides Sub WndProc(ByRef m As Message)
              >
              > Const WM_ACTIVATEAPP As Integer = &H1C
              >
              > Select Case m.Msg
              > Case WM_ACTIVATEAPP
              > If m.WParam.ToInt3 2 = 1 Then
              > Dim d As New SideStep.Rejoin (AddressOf Me.OnAppActivat e)
              > Dim ss As New SideStep(d)
              > ss.Skip()
              > End If
              > . . .
              > End Select
              >
              > MyBase.WndProc( m)
              >
              > End Sub
              >
              > Regards,
              > Phill W.
              >
              > RMT wrote:[color=green]
              > > Yes, invoking or begininvoking is the way to go - do you have any code?
              > > seems like you are almost there.......... .........
              > >
              > >
              > >
              > > "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> escribió en el mensaje
              > > news:e6bp4o$89l $1@yarrow.open. ac.uk...[color=darkred]
              > >> Can anyone recommend a good [web-based] reference on Threading (Framework
              > >> 1.1)?
              > >>
              > >> I'm particularly confused about how to [safely] call a method in my "main"
              > >> thread from another one. - or more, one day ;-)
              > >>
              > >> I've tried creating delegates to main thread methods and Invoking themand
              > >> BeginInvoking them, but I've had some quite /horrendous/ results ...
              > >>
              > >> TIA,
              > >> Phill W. [/color]
              > >
              > >[/color][/color]

              Comment

              • Michel Posseth [MCP]

                #8
                Re: Threading

                Hello Phill,

                i would recomend this one


                i believe this is what you are looking for ( with example sourcecode in
                VB.net 2003 )
                http://msdn.microsoft.com/msdnmag/is...asicInstincts/



                I would also recomend you the core reference guide of Francesco Balena (
                you probably know him from his excellent VB6 core reference )
                well he also wrote te official core references for VB.Net , VB.Net 2003 and
                VB.Net 2005

                Threading and everything you need to know about .Net is in these books extra
                advantage he did not forget us "old" VB6 programmers and gives us some
                special attention here and there ;-)

                regards

                Michel Posseth [MCP]




                "Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> schreef in bericht
                news:e6bp4o$89l $1@yarrow.open. ac.uk...[color=blue]
                > Can anyone recommend a good [web-based] reference on Threading (Framework
                > 1.1)?
                >
                > I'm particularly confused about how to [safely] call a method in my "main"
                > thread from another one. - or more, one day ;-)
                >
                > I've tried creating delegates to main thread methods and Invoking them and
                > BeginInvoking them, but I've had some quite /horrendous/ results ...
                >
                > TIA,
                > Phill W.[/color]


                Comment

                Working...