How to correctly Dispose SmartPart from Workspace

Mar 5, 2012 at 4:27 PM

What's the correct way to Dispose Views (smartparts) shown in DeckWorkspaces ?

Calling Dispose raise some errors at Runtime?

not Calling it means not disposing at all.

 

Do I need to use Close or DeActivate ?

 

What's the better way to do it ?

Coordinator
Mar 5, 2012 at 4:36 PM

You can call Dispose() as you would normally, but it is important to *remove* the item from the DI container before doing so, otherwise you'll end up getting an ObjectDisposedException when the framework tries to use the Disposed item.  This is probably a scenario I should add better support for, as right now the general assumption is that you'll create the object, but never remove it.

Mar 5, 2012 at 5:37 PM

Thanks Chris for your reply

 

That's what I was looking for inside the source code.

 

So I've got a question : in OpenNETCF.IoC.UI.Workspace, inside OnClose(ISmartPart smartPart) method, at the end it's coded 

            RootWorkItem.SmartParts.Remove(smartPart);

            smartPart.Dispose();

 

I think its redundant because in the Remove method of ManagedObjectCollection, IDisposable object are Disposed.

Finally I used Close in my application because, just calling Dispose cause runtime error on device when Showing another smartPart 'cause the IoC.UI Fwk try to make the disposed (but not removed) smartPart not visible.

Vincent

Mar 6, 2012 at 6:44 AM
Edited Mar 14, 2012 at 8:25 AM

Hi Chris,

 

Still a little question. It seems that it's still not enough. Indeed, in Workspace.Close method, the smartpart I want to release is correctly removed and disposed (even twice), but it's still referenced as the ActiveSmartPart so when I try to display an other smartpart, it throws the ObjectDisposedException when in the Activate() method the framework find that the new smartpart is not the Active one, so he think it should DeActivate the Active one but it's the one he previously Remove, Closed and Disposed.

So I think that the Workspace.Close method should DeActivate the View and the DeActivate should make ActiveSmartPart to a null value.

Any comments ?

 

Here's a simple repro :

 

    public partial class Form1 : Form
    {
        private Workspace wks = new Workspace();
        private SmartPart spA = new SmartPart();
        private SmartPart spB = new SmartPart();

        public Form1()
        {
            InitializeComponent();
            Controls.Add(wks);
            RootWorkItem.Workspaces.Add(wks);
            wks.Show(spA);
            wks.Close(spA);
            wks.Show(spB);
        }
    }

As the Workspace.ActiveSmartPart has a protected set I have no way with the framework to Dispose a SmartPart a soon as I don't need it anymore except by deriving my Workspaces from your base Workspace class.

 

What's your opinion ?

Jun 21, 2012 at 9:56 AM
Edited Jun 21, 2012 at 9:58 AM

Hi everibody, is this project still alive ?

I'm facing the same problem as Vincent :

Can't understand how to add again a smartpart previously closed (which remains 'IsDisposed=True' as ActiveSmartPart).

I'm not able to run twice the SmartPartTests sample, too.

Any suggestion ?

Thanks

Marco

Coordinator
Aug 7, 2012 at 3:26 PM

First, sorry for my delayed reply.  I've been busy with the ORM project and some other things.

Vincent, yes, you're right.  The Dispose was redundant, and the ActiveSmartPart should be getting cleared.  I've checked in changes for these and updated the WorkspaceDisposal sample app to show valid Closing of a SmartPart.

Marco, yes, the project is still active.  There isn't a lot of activity in the code base because I think the library is pretty mature.  If there are missing features, I'd be happy to add them, but we currently use IoC in all of our commercial products, so it's heavily used across many platforms.  I've updated the SmartPartTests app to better handle when a SmartPart is Disposed.  I'm not having any trouble running it multiple times.