Home All Groups Group Topic Archive Search About
Author
9 Jun 2006 12:24 PM
Phill W.
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.

Author
9 Jun 2006 1:08 PM
AlanT
Phill W. wrote:
> 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.

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

http://msdn.microsoft.com/msdnmag/find/?type=Au&phrase=Ted%20Pattison&words=exact


hth,
Alan
Author
9 Jun 2006 1:14 PM
RMT
Yes, invoking or begininvoking is the way to go - do you have any code?
seems like you are almost there...................



Show quoteHide quote
"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...
> 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.
Author
9 Jun 2006 2:27 PM
Phill W.
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_oCallbackDelegate = oCB
     m_iDelay = iDelay
   End Sub

   Public Delegate Sub Rejoin()

   Public Sub Skip()
     Dim t As New Threading.Thread(AddressOf Me.Run)
     t.Name = "WM_ActivateApp - SideStep"
     t.Start()
   End Sub

   Private Sub Run()
     System.Threading.Thread.Sleep(m_iDelay)
     m_oCallbackDelegate.Invoke()
   End Sub

   Private m_oCallbackDelegate 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.ToInt32 = 1 Then
       Dim d As New SideStep.Rejoin(AddressOf Me.OnAppActivate)
       Dim ss As New SideStep(d)
       ss.Skip()
     End If
   . . .
   End Select

   MyBase.WndProc(m)

End Sub

Regards,
    Phill  W.

RMT wrote:
Show quoteHide quote
> 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...
>> 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.
>
>
Author
9 Jun 2006 5:02 PM
Brian Gideon
Phill,

The problem is that the m_oCallbackDelegate 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_oCallbackDelegate 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_oCallbackDelegate.Invoke pass the delegate as a parameter into
yourForm.BeginInvoke.  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:
Show quoteHide quote
> 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_oCallbackDelegate = oCB
>      m_iDelay = iDelay
>    End Sub
>
>    Public Delegate Sub Rejoin()
>
>    Public Sub Skip()
>      Dim t As New Threading.Thread(AddressOf Me.Run)
>      t.Name = "WM_ActivateApp - SideStep"
>      t.Start()
>    End Sub
>
>    Private Sub Run()
>      System.Threading.Thread.Sleep(m_iDelay)
>      m_oCallbackDelegate.Invoke()
>    End Sub
>
>    Private m_oCallbackDelegate 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.ToInt32 = 1 Then
>        Dim d As New SideStep.Rejoin(AddressOf Me.OnAppActivate)
>        Dim ss As New SideStep(d)
>        ss.Skip()
>      End If
>    . . .
>    End Select
>
>    MyBase.WndProc(m)
>
> End Sub
>
> Regards,
>     Phill  W.
>
> RMT wrote:
> > 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...
> >> 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.
> >
> >
Author
9 Jun 2006 1:27 PM
Cor Ligthert [MVP]
Phill,

In the dotnet newsgroups there is only one constant reference

http://www.yoda.arachsys.com/csharp/threads/

I hope this helps,

Cor

Show quoteHide quote
"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...
> 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.
Author
9 Jun 2006 1:48 PM
Brian Gideon
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.com/threading/>

Brian

Phill W. wrote:
Show quoteHide quote
> 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.
Author
10 Jun 2006 7:48 PM
Michel Posseth [MCP]
Hello Phill,

i would recomend this one
http://addressof.com/blog/archive/2005/02/10/1287.aspx

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



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]




Show quoteHide quote
"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...
> 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.