How to kill an asynchronous delegate?

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

    How to kill an asynchronous delegate?

    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 ServerThreadSta rt.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.Remo ve(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 ListenForConnec tionDelegate(By Val PortNo As Int16)

    Friend Class ServerThreadSta rter

    Friend Shared Sub StartServer(ByV al PortNo As Int16)
    Dim ar As IAsyncResult
    Dim dlgtServer As New ListenForConnec tionDelegate(Ad dressOf
    ServerThread.Li stenForConnecti on)

    ar = dlgtServer.Begi nInvoke(PortNo, AddressOf
    CallbackFromSer verThread, dlgtServer)

    ar = Nothing
    dlgtServer = Nothing
    End Sub

    Private Shared Sub CallbackFromSer verThread(ByVal ar As
    IAsyncResult)
    ' Retrieve the delegate
    Dim dlgt As ListenForConnec tionDelegate = CType(ar.AsyncS tate,
    ListenForConnec tionDelegate)
    ' Call EndInvoke
    dlgt.EndInvoke( ar)
    dlgt = Nothing
    End Sub
    End Class

    Friend Class ServerThread
    Friend Shared Sub ListenForConnec tion(ByVal PortNo As Int16)
    ...
    End Sub
    End Class
  • cecil@ceciltech.com

    #2
    Re: How to kill an asynchronous delegate?

    Stewart,[color=blue]
    >From the looks of what you are doing you would be better off using[/color]
    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:[color=blue]
    > Hey Group,
    >
    > Hoping someone can help me out. I have some code which starts up[/color]
    some[color=blue]
    > asynchronous code using a delegate. The code is below. Basically my
    > main code (not shown) calls ServerThreadSta rt.StartServer to start[/color]
    the[color=blue]
    > 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.Remo ve(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 ListenForConnec tionDelegate(By Val PortNo As[/color]
    Int16)[color=blue]
    >
    > Friend Class ServerThreadSta rter
    >
    > Friend Shared Sub StartServer(ByV al PortNo As Int16)
    > Dim ar As IAsyncResult
    > Dim dlgtServer As New ListenForConnec tionDelegate(Ad dressOf
    > ServerThread.Li stenForConnecti on)
    >
    > ar = dlgtServer.Begi nInvoke(PortNo, AddressOf
    > CallbackFromSer verThread, dlgtServer)
    >
    > ar = Nothing
    > dlgtServer = Nothing
    > End Sub
    >
    > Private Shared Sub CallbackFromSer verThread(ByVal ar As
    > IAsyncResult)
    > ' Retrieve the delegate
    > Dim dlgt As ListenForConnec tionDelegate =[/color]
    CType(ar.AsyncS tate,[color=blue]
    > ListenForConnec tionDelegate)
    > ' Call EndInvoke
    > dlgt.EndInvoke( ar)
    > dlgt = Nothing
    > End Sub
    > End Class
    >
    > Friend Class ServerThread
    > Friend Shared Sub ListenForConnec tion(ByVal PortNo As Int16)
    > ...
    > End Sub
    > End Class[/color]

    Comment

    • windsurfing_stew@yahoo.com.au

      #3
      Re: How to kill an asynchronous delegate?

      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 ServerThreadSta rter

      Private Shared thrd As Thread

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

      svr.PortNo = 8989
      If thrd Is Nothing Then
      thrd = New Thread(AddressO f svr.ListenForCo nnection)
      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

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to kill an asynchronous delegate?

        <windsurfing_st ew@yahoo.com.au > schrieb[color=blue]
        > thrd.Abort()[/color]

        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/>


        Comment

        Working...