none
How to safely dispose Dispatcher, DispatcherQueue and Interleave RRS feed

  • Question

  • Hi All,

    I have a class say "MyClass" that has a single dispatcher and a single dispatcher queue. The class exposes a single PortSet to which clients can post messages to. In "MyClass", I have created persistent receivers for those messages and attached to the dispatcher queue. I have also added all the receivers as part of a single Interleave. Now when the client thinks it is done using "MyClass" class, I want the client to destroy the class safely. There are three things that needs to destroy here which are the dispatcher, the dispatcher queue and the interleave. What is the best way to do that? I thought of the following but had a questions about them.

    1) Have a message called "DisposeComponent" and and it as part of the TeardownReceiverGroup of the interleave and then call the Dispatcher.Dispose() and DispatcherQueue.Dispose() like the following

    public void DisposeComponentHandler(DisposeComponent message)
    {
        Dispatcher.Dispose();
        DispatcherQueue.Dispose();
    }

    Is it safe to call dispose on both the dispatcher and dispatcher queue as part of the execution of the teardown receiver code?

    2) Make the class implement IDispose and Dispose both the Dispatcher and the DispatcherQueue as given below.

    public void Dispose()
    {
        Dispatcher.Dispose();
        DispatcherQueue.Dispose();
    }

    However, this may not be safe as there may be some receivers that may be executing and also there is no way to dispose those receivers for the messages.

    Please let me know the best possible solution for this.

    Thanks,

    Venkat

    Thursday, October 4, 2012 9:02 AM
    Moderator

Answers

  • Hi All.

    I happened to read the discussions in the link below.

    http://channel9.msdn.com/shows/Going+Deep/CCR-Programming-Jeffrey-Richter-and-George-Chrysanthakopoulos/

    I infer from the discussion that calling a Dispose on the DispatcherQueue will not dispose the interleave. The right way that was suggested by the discussion is that I need to first tear down the interleave, then dispose the dispatcher queue. Now, the dispose code of my class will look like the following

    public void Dispose()
    {
        // Tear down the interleave
        OperationsPort.Post(new EmptyValue());
    
        // dispose the DispatcherQueue
        TaskQueue.Dispose();
    }

    Assume that there are now currently no taks that are running in the DispatcherQueue and then a Dispose is called and there will be no more messages posted to the component once it is disposed or while it is disposing. Tearing down the interleave is an asynchronous operartion. So, should we wait for the tearing down to complete (by possibly having a flag and doing a spin lock or a response port for the teardown message and wait on the response port) before we call the Dispose of the DispatcherQueue? Are both the tearing down of receiver and disposing of the DispatcherQueue independent and we do not have to worry about synchronizing both of them and having things done the way I described in the code above be sufficient.

    Please clarify.

    Thanks,

    Venkat

    Sunday, October 7, 2012 8:12 AM
    Moderator

All replies

  • Hi All,

    I have another added question. If I call a Dispose on DispatcherQueue, will all the tasks (persistent Receiver task, non-persistent reciever task, choice task, interleave task etc) that are enqueued to the DispatcherQueue be disposed off by GC as well? I am aware from the many posts that I read in the forum that the Dispatcher gets disposed but however I am not sure about the other tasks.

    Thanks,

    Venkat



    Saturday, October 6, 2012 4:16 AM
    Moderator
  • Hi All.

    I happened to read the discussions in the link below.

    http://channel9.msdn.com/shows/Going+Deep/CCR-Programming-Jeffrey-Richter-and-George-Chrysanthakopoulos/

    I infer from the discussion that calling a Dispose on the DispatcherQueue will not dispose the interleave. The right way that was suggested by the discussion is that I need to first tear down the interleave, then dispose the dispatcher queue. Now, the dispose code of my class will look like the following

    public void Dispose()
    {
        // Tear down the interleave
        OperationsPort.Post(new EmptyValue());
    
        // dispose the DispatcherQueue
        TaskQueue.Dispose();
    }

    Assume that there are now currently no taks that are running in the DispatcherQueue and then a Dispose is called and there will be no more messages posted to the component once it is disposed or while it is disposing. Tearing down the interleave is an asynchronous operartion. So, should we wait for the tearing down to complete (by possibly having a flag and doing a spin lock or a response port for the teardown message and wait on the response port) before we call the Dispose of the DispatcherQueue? Are both the tearing down of receiver and disposing of the DispatcherQueue independent and we do not have to worry about synchronizing both of them and having things done the way I described in the code above be sufficient.

    Please clarify.

    Thanks,

    Venkat

    Sunday, October 7, 2012 8:12 AM
    Moderator
  • Hi All.

    I was wondering if anyone had an opportunity to look at these.

    Thanks,

    Venkat

    Saturday, October 20, 2012 6:36 AM
    Moderator