Deactivating and Activating WorkItems

Topics: CAB & Smart Client Software Factory
Jan 13, 2006 at 7:03 AM
originally posted by: SamSneed

In my application, I two buttons that create a WorkItem that adds some smartparts to a workspace. Once WorkItem A is up and running. I want to click on Button B to deactivate WorkItem A a.Deactivate() and then create WorkItem B. Then when I click on Button A, again, I want to deactivate WorkItem B b.Deactivate() and reactivate WorkItem A a.Activate() if, it already exists.

The problem I am having is that I have overrides for OnDeactivated() and OnActivated() that are not hit the first time I call Deactivate() and Activate() for either of the WorkItems. Upon the second time that the calls are made and everytime there after, I hit my break points.

My question is why do I not hit my breakpoints in the WorkItem overrides the first time <WorkItem>.Deactivate() and <WorkItem>.Activate() are called?
Jan 13, 2006 at 10:16 AM
originally posted by: BradWilsonMSFT

You will only get the call if the state changes; meaning, if you have a de-activated WorkItem and call Deactivate() on it, you will NOT get a notice of the second call. Are you sure your WorkItems are in the state you think they are?
Jan 13, 2006 at 11:47 AM
originally posted by: SamSneed

I assumed when a new WorkItem is injected into the RootWorkItem it is considered active and calling Deactivate() for that new WorkItem immediately after it is injected would cause the OnDeactivated() override to get hit. And, if I then call Activate() immediately after it would trigger OnActivated(). But, what I am seeing consistently is that it is not until the 2nd time I call Deactivate() for the new WorkItem that it triggers the OnDeactivated override. It's only in the case of a NEWLY CREATED WorkItem.

What is the initailized state when one creates a new WorkItem via this.<parentWorkItem>.WorkItems.AddNew<type>(id)?

I will post sample code after this post.
Jan 13, 2006 at 11:47 AM
originally posted by: SamSneed

SAMPLE CODE:

Below is some sample code:

//
//ModuleInit Code
//

public override void Load()
{
base.Load();
MessageBox.Show("About to Activate WorkItem A");
this.ActivateWorkItemA();

MessageBox.Show("About to Activate WorkItem B");
this.ActivateWorkItemB();

MessageBox.Show("About to Reactivate WorkItem A");
this.ActivateWorkItemA();

MessageBox.Show("About to Reactivate WorkItem B");
this.ActivateWorkItemB();
}

private void ActivateWorkItemA()
{
//Deactivate workitem B
if (this.parentWorkItem.WorkItems.Contains("WorkItemB"))
{
SneedModWorkItem2 workItemB = this.parentWorkItem.WorkItems.Get<SneedModWorkItem2>("WorkItemB");
workItemB.Deactivate();
}

//Work with workitem A
SneedModWorkItem workItemA = this.parentWorkItem.WorkItems.Get<SneedModWorkItem>("WorkItemA");

if (!this.parentWorkItem.WorkItems.Contains("WorkItemA"))
{
workItemA = this.parentWorkItem.WorkItems.AddNew<SneedModWorkItem>("WorkItemA");
workItemA.ID = "WorkItemA";
}
else
{
workItemA.Activate();
}
}

private void ActivateWorkItemB()
{
//Deactivate workitem A
if (this.parentWorkItem.WorkItems.Contains("WorkItemA"))
{
SneedModWorkItem workItemA = this.parentWorkItem.WorkItems.Get<SneedModWorkItem>("WorkItemA");
workItemA.Deactivate();
}

//Work with workitem B
SneedModWorkItem2 workItemB = this.parentWorkItem.WorkItems.Get<SneedModWorkItem2>("WorkItemB");

if (!this.parentWorkItem.WorkItems.Contains("WorkItemB"))
{
workItemB = this.parentWorkItem.WorkItems.AddNew<SneedModWorkItem2>("WorkItemB");
workItemB.ID = "WorkItemB";
}
else
{
workItemB.Activate();
}
}


//
// Contents of SneedModWorkItem and SneedModWorkItem2
// Where <ID> corresponds to "A" (SneedModWorkItem) or "B" (SneedModWorkItem2)
//

protected override void OnActivated()
{
System.Windows.Forms.MessageBox.Show("Activated WorkItem <ID>.");
base.OnActivated();
}

protected override void OnDeactivated()
{
System.Windows.Forms.MessageBox.Show("Dactivated WorkItem <ID>.");
base.OnDeactivated();
}
Jan 13, 2006 at 11:50 AM
originally posted by: SamSneed

If you look at the Load() override, I expect that the first time I call ActivateWorkItemB() that WorkItemA's
OnDeactivated() override would be hit. But, it is not. It is not until the second time I call ActivateWorkItemB()
that it is hit.
Jan 14, 2006 at 6:29 AM
originally posted by: BradWilsonMSFT

The initial state of a WorkItem is inactive, not active.
Jan 14, 2006 at 8:58 AM
originally posted by: SamSneed

Thanks! Is there anyway other than calling <WorkItem>.Activate() to change this initial state? When does the state change to "active" if one does not call Activate()?
Jan 14, 2006 at 12:39 PM
originally posted by: DLorenz

If you do not explicity call WorkItem.Activate, then you would have to set focus to a SmartPart that it houses. If a SmartPart gets focus, it will automatically activate its WorkItem.
Jan 14, 2006 at 1:27 PM
originally posted by: BradWilsonMSFT

Right. And further, the presence of the SimpleWorkItemActivationService ensures that only one WorkItem is ever active at once.
Feb 2, 2006 at 7:26 AM
originally posted by: suluhs

What about if the WorkItem register a CommandHandler and the user execute that command ? Should the WorkItem status be changed to active ? Currently it is not. Is this by design ?
Feb 2, 2006 at 8:00 AM
originally posted by: BradWilsonMSFT

Yes, this is by design.