Threading

  • Thread starter Thread starter Phill W.
  • Start date Start date
P

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.
 
Phill said:
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 Pattison&words=exact


hth,
Alan
 
Yes, invoking or begininvoking is the way to go - do you have any code?
seems like you are almost there...................
 
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
 
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.
Yes, invoking or begininvoking is the way to go - do you have any code?
seems like you are almost there...................



Phill W. said:
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.
 
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
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.
Yes, invoking or begininvoking is the way to go - do you have any code?
seems like you are almost there...................



Phill W. said:
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.
 
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]
 
Back
Top