HighTechTalks DotNet Forums  

How to kill an asynchronous delegate?

Dotnet Framework (Remoting) microsoft.public.dotnet.framework.remoting


Discuss How to kill an asynchronous delegate? in the Dotnet Framework (Remoting) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default How to kill an asynchronous delegate? - 12-16-2004 , 08:01 PM






Hey Group,

Hoping someone can help me out. I have some code which starts up some
asynchronous code using a delegate. The code is below. Basically my
main code (not shown) calls ServerThreadStart.StartServer to start the
server running asynchronously. This works fine. Shouldn't be any
problems here.

My question is how can I get my code to kill this code running
asynchronously? There is a dlgtServer.Remove(Delegate, Delegate)
command but there seems to be little of value in MSDN to help you
determine the two parameters.

Hope you can help,

Stewart



Friend Delegate Sub ListenForConnectionDelegate(ByVal PortNo As Int16)

Friend Class ServerThreadStarter

Friend Shared Sub StartServer(ByVal PortNo As Int16)
Dim ar As IAsyncResult
Dim dlgtServer As New ListenForConnectionDelegate(AddressOf
ServerThread.ListenForConnection)

ar = dlgtServer.BeginInvoke(PortNo, AddressOf
CallbackFromServerThread, dlgtServer)

ar = Nothing
dlgtServer = Nothing
End Sub

Private Shared Sub CallbackFromServerThread(ByVal ar As
IAsyncResult)
' Retrieve the delegate
Dim dlgt As ListenForConnectionDelegate = CType(ar.AsyncState,
ListenForConnectionDelegate)
' Call EndInvoke
dlgt.EndInvoke(ar)
dlgt = Nothing
End Sub
End Class

Friend Class ServerThread
Friend Shared Sub ListenForConnection(ByVal PortNo As Int16)
...
End Sub
End Class

Reply With Quote
  #2  
Old   
AT
 
Posts: n/a

Default Re: How to kill an asynchronous delegate? - 12-16-2004 , 10:17 PM






Stewart,
Quote:
From the looks of what you are doing you would be better off using
the Thread class to spawn your worker thread. By using a delegate's
BeginInvoke method you are making use of a thread from the thread pool.
This method of multithreading is optimized for shorter running tasks
and has no built in way to allow you to kill the thread. I am sure it
could be done, but it would be a hack. If you use the Thread class you
have much greater control including the ability to kill the thread.
The method you start the thread with can not take any methods so remove
portno from the args of your listen method and make it a private field
of the class that is set in the constructor and make the listen method
an instance method instead of shared.

Hope this helps

Cecil Howell MCSD, MCAD.Net, MCT


Stewart wrote:
Quote:
Hey Group,

Hoping someone can help me out. I have some code which starts up
some
asynchronous code using a delegate. The code is below. Basically my
main code (not shown) calls ServerThreadStart.StartServer to start
the
server running asynchronously. This works fine. Shouldn't be any
problems here.

My question is how can I get my code to kill this code running
asynchronously? There is a dlgtServer.Remove(Delegate, Delegate)
command but there seems to be little of value in MSDN to help you
determine the two parameters.

Hope you can help,

Stewart



Friend Delegate Sub ListenForConnectionDelegate(ByVal PortNo As
Int16)

Friend Class ServerThreadStarter

Friend Shared Sub StartServer(ByVal PortNo As Int16)
Dim ar As IAsyncResult
Dim dlgtServer As New ListenForConnectionDelegate(AddressOf
ServerThread.ListenForConnection)

ar = dlgtServer.BeginInvoke(PortNo, AddressOf
CallbackFromServerThread, dlgtServer)

ar = Nothing
dlgtServer = Nothing
End Sub

Private Shared Sub CallbackFromServerThread(ByVal ar As
IAsyncResult)
' Retrieve the delegate
Dim dlgt As ListenForConnectionDelegate =
CType(ar.AsyncState,
ListenForConnectionDelegate)
' Call EndInvoke
dlgt.EndInvoke(ar)
dlgt = Nothing
End Sub
End Class

Friend Class ServerThread
Friend Shared Sub ListenForConnection(ByVal PortNo As Int16)
...
End Sub
End Class


Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default Re: How to kill an asynchronous delegate? - 12-19-2004 , 07:25 PM



Thanks for your help. I've updated this code to work with the
Threading object rather than using delegates. Notice I've added a
StopServer method.

Friend Class ServerThreadStarter

Private Shared thrd As Thread

Friend Shared Sub StartServer(ByVal PortNo As Int16)
Dim svr As ServerThread

svr.PortNo = 8989
If thrd Is Nothing Then
thrd = New Thread(AddressOf svr.ListenForConnection)
thrd.Start()
End If
End Sub

Friend Shared Sub StopServer()
If Not thrd Is Nothing Then
thrd.Abort()
thrd.Join()
End If
End Sub

End Class


Reply With Quote
  #4  
Old   
Herfried K. Wagner [MVP]
 
Posts: n/a

Default Re: How to kill an asynchronous delegate? - 12-26-2004 , 05:11 PM



<windsurfing_stew (AT) yahoo (DOT) com.au> schrieb
Quote:
thrd.Abort()
If you have control over the thread's implementation/work:

How To Stop a Thread in .NET (and Why 'Thread.Abort' is Evil)
<URL:http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.