By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

How to kill an asynchronous delegate?

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Stewart,
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: 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


Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
<wi**************@yahoo.com.au> schrieb
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/>
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.