Terminating child work item

Topics: CAB & Smart Client Software Factory
Oct 15, 2005 at 2:39 AM
originally posted by: sassanow

I've come across an issue in a project that I'm working on that I'm hoping someone can spot my mistake.

My project is in VB.net using CAB CTP2, VS 2005 RC.

I've got a parent WorkItem that creates a child WorkItem, when the child terminates itself (at user's request), it still remains in the parent's WorkItems collection. If I try to dispose of it, it tells me it's already terminated/disposed. If I try to add a new child (same WorkItem as the first), it tells me it already exists in the parent collection.

How do you properly teminate a child WorkItem so that it no longer exists in the parent's WorkItem collection?
Oct 15, 2005 at 4:00 AM
originally posted by: BradWilsonMSFT

There is are a couple bugs in WorkItem termination in the October CTP. This will be fixed for the final release. The behavior you expect (terminating a WorkItem removes it from its parent WorkItem) is correct, and the code will behave properly in the final release.

Thanks!
Oct 15, 2005 at 4:46 AM
originally posted by: AgentOrange

I just ran into that problem myself. What should we do in the meantime until the final release? Create unique id's for each child workitem (so they don't step on each other in the WorkItems collection)?
Oct 15, 2005 at 4:50 AM
originally posted by: BradWilsonMSFT

You cannot reuse an instance of a WorkItem (nor will you ever be able to).

If your WorkItems are unnamed, then they won't step on each other. If they are named, then you're up against the limitation that two items in a WorkItem cannot share the same name.

There is another related bug, by the way, which causes a parent WorkItem to throw an exception when it cleans up, if you've terminated any of its child work items. (It's related, because the terminated WorkItem gets Dispose called on it again, and it throws an exception to say "I've already been terminated/disposed".)
Oct 15, 2005 at 4:53 AM
originally posted by: AgentOrange

Makes perfect sense. Thanks for the quick response!
Oct 15, 2005 at 7:01 AM
originally posted by: bohema

Aint it possible to update workitem termination routine to work propelrly? Until final release? :)
Oct 15, 2005 at 7:13 AM
originally posted by: BradWilsonMSFT

With the final release being just a few weeks away, I'm hesitant to list complex patches on the message boards for the October CTP. If this is really causing lots of issues, you can find a quick fix for the throwing problem (this biggest issue) here:

http://www.gotdotnet.com/codegallery/messageboard/thread.aspx?id=22f72167-af95-44ce-a6ca-f2eafbf2653c&mbid=f095dc16-95cc-4b64-9229-3510390aa521&threadid=ec4f6747-fab2-4aa4-b8d6-97fe61d192a9
Oct 15, 2005 at 7:31 AM
originally posted by: bohema

that issue is not the problem for me. problem is that WorkItem stays in the Parent Collection (or i missunderstood something).

My scenario is:
I create work item with key (depends on data object), and show it on windowsworkscpace.
So then i close form with windowworkspace, I terminate this WorkItem. But in my scenario it is possible to recreate WorkItem with same key -- so here i have lot of problems. Parent WorkItem finds old terminated item and cant recreate new form. :(

may be it is another way for may scenario?
Oct 15, 2005 at 8:17 AM
originally posted by: BradWilsonMSFT

Yeah, I was afraid of that. That's the bug that required more than a simple change.

Can you wait a few weeks for the fix in the final release?
Oct 15, 2005 at 9:01 AM
originally posted by: bohema

if it possible to post this bug fix earlier it would be very nice. :)
now i removed key form workitem, looks bad, but works...

cant you release just some bug fixes from time to time until final release?

It is all about that CAB rox and we cant stop using it. :)
Oct 15, 2005 at 2:15 PM
originally posted by: sassanow

I second that last thought... CAB ROX!
Oct 15, 2005 at 3:53 PM
originally posted by: BradWilsonMSFT

All right, all right. Never let it said we're not suckers for good vibes. :)

To fix this set of bugs, you need to make two changes. Here is the new body for WorkItem.Dispose(bool):

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (status == WorkItemStatus.Terminated)
return;

ChangeStatus(WorkItemStatus.Terminated);

if (parent != null)
{
List<object> ids = new List<object>();

foreach (KeyValuePair<object, object> pair in parent.locator)
if (pair.Value == this)
ids.Add(pair.Key);

foreach (object id in ids)
parent.locator.Remove(id);

parent.lifetime.Remove(this);
}

lifetime.Remove(this);

List<object> lifetimeObjects = new List<object>();
lifetimeObjects.AddRange(lifetime);

foreach (object obj in lifetimeObjects)
if (lifetime.Contains(obj))
builder.TearDown(locator, obj);

lifetime.Dispose();
OnDisposed();
}
}

And here is the new body for WorkItem.Terminate():

public void Terminate()
{
ThrowIfWorkItemTerminated();
Dispose();
}

Hopefully that'll help you out. :)
Oct 15, 2005 at 4:50 PM
originally posted by: Phyrefly

Thanks Brad... that code helped a little. It works fine now - you can terminate a workitem and then create a new one with the same key again and it doesn't complain, but now there is a new problem->>
The Dispose method of the lifetime container crashes on the foreach saying "Collection was modified; enumeration operation may not execute"

this is the code snippet:

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
foreach (object o in items)
{
IDisposable d = o as IDisposable;

if (d != null)
d.Dispose();
}
}
}


Is there a quick and simple fix for this? - Can I just catch the exception and throw it away? Please help, and by the way CAB is cool!!! u guys rock! can't wait for the final release ;)
Oct 15, 2005 at 5:04 PM
originally posted by: BradWilsonMSFT

You should be able to change:

foreach (object o in items)

to:

foreach (object o in items.ToArray())

Yet another bug we recently found and fixed. ;)
Oct 16, 2005 at 1:43 AM
originally posted by: bohema

thank you very much! :)