locked
Using Closed Event. How? RRS feed

  • Question

  • I am struggling with this and I don't know why! On another note why the TurnStarting the only event exposed in the designer?

     

    I am adding

     

    this.TempPermQuestion.Closed += new System.EventHandler<System.Workflow.ComponentModel.ActivityExecutionStatusChangedEventArgs>(this.TempPermQuestion_Closed);

    this.HoursWorkedQuestion.Closed += new System.EventHandler<System.Workflow.ComponentModel.ActivityExecutionStatusChangedEventArgs>(this.HoursWorkedQuestion_Closed);

     

    and then writing 2 methods

     

    private void HoursWorkedQuestion_Closed(object sender, EventArgs eventArgs)

    {

    }

    private void TempPermQuestion_Closed(object sender, EventArgs eventArgs)

    {

    }

     

    I can see this pattern used in the CallRouter example.

     

    First why doesn't this work? I have added the events to the code-behind (like CallRouter), to the constructor and to a code activity at the start of the activity but none of them work. I get "activity with name "" not found" or it just bombs out of the sequence to the start of the debugger with not even an exception.

     

    This is a SpeechServerSequenceActivity in an Activity Library by the way!

     

    Why can't it be easy to add handling for a Closed event, just like the TurnStarting. Seems stupid to me !!

     

    Cliff

     

    Monday, June 4, 2007 5:55 PM

Answers

  • Cliff,

    You are right. The closed event does not seem to work from the inside the Speech Sequence library.

    An alternate(more like a workaround) that I tried and worked is to subscribe to the closed event from the main workflow (the one that hosts the Speech Sequence library)

     

    //Code in the constructor of the Main Workflow.

     

    this.SpeechSequenceActivity.Activities["ActivityName"].Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(ActivityName_Closed);

     

    private void ActivityName_Closed(object sender, EventArgs e)

    {

          //this might not be the cleanest of the solution but is a workaround

    }

     

     

    Sujeet

    Wednesday, June 6, 2007 7:40 PM

All replies

  • Cliff,

    You're right that there is only limited designer support for events. For a QuestionAnswerActivity (which I'm assuming are the activities you're using), the designer semi-automates only the plumbing for a TurnStarting event.

     

    To use other events (like the Closed event) you need to do as you have described to add code to register a handler for the event, and define the event handler itself. The only thing I see that might be causing problems is one argument in your handler. In the code you show, the second arg is of type EventArgs. In the Closed handler in the CallRouter sample, the second arg is of type ActivityExecutionStatusChangedEventArgs (see below). I can't guarantee that this is why you're having problems, but start with this change, and see if your problems clear up. I have used the Closed event and a couple of others without difficulty.

     

    Code Snippet

    private void askRoute_Closed(object sender, ActivityExecutionStatusChangedEventArgs activityExecutionStatusChangedEventArgs)

    {

         /// body of handler

    }

     

     

    Monday, June 4, 2007 6:37 PM
  • Hi

     

    I did try that first but the CallRouter example uses

     

    /// <summary>

    /// confirmTrackingNumber_Closed - Check if the

    /// caller indicated the tracking number was incorrect. If

    /// so, increment a tries counter. If not, consider the tracking

    /// number confirmed and look it up in an XML file.

    /// </summary>

    /// <param name="sender">The source of the event</param>

    /// <param name="eventArgs">The description of the event</param>

    private void confirmTrackingNumber_Closed(object sender, EventArgs eventArgs)

    {

         RecognitionResult result = this.confirmTrackingNumber.RecognitionResult;

         if (result != null)

     

     

    So I tried that.

     

    I suspect that its because I am in a Child Activity in an Activity Library. I don't know why I think that Smile but 99% of all the issues I have had have been caused by "things" that work normally but fall apart when used in a Library!

     

    I don't have time to investigate now, I may come back to it if I get time. I have cludged it and get my values in a code activity that fires after the activity I wanted to use.

     

    Cliff

     

    Monday, June 4, 2007 9:02 PM
  • Cliff,

    Actually, we're both right. The CallRouter sample uses EventArgs in some Closed event handlers but uses ActivityExecutionStatusChangedEventArgs class I mentioned in at least one other.

    When you get a chance, please give me a bit more detail about the library. From your earlier post, it sounds like the library consists of a SpeechSequenceActivity with, presumably, one or more child activities. If it's the SpeechSequenceActivity that is raising the Closed event, that means all of the child events have completed execution, and have likely been destroyed, which probably explains your remark that the activity with some name wasn't found.

    Mark

    Tuesday, June 5, 2007 12:36 AM
  • Cliff,

    You are right. The closed event does not seem to work from the inside the Speech Sequence library.

    An alternate(more like a workaround) that I tried and worked is to subscribe to the closed event from the main workflow (the one that hosts the Speech Sequence library)

     

    //Code in the constructor of the Main Workflow.

     

    this.SpeechSequenceActivity.Activities["ActivityName"].Closed += new EventHandler<ActivityExecutionStatusChangedEventArgs>(ActivityName_Closed);

     

    private void ActivityName_Closed(object sender, EventArgs e)

    {

          //this might not be the cleanest of the solution but is a workaround

    }

     

     

    Sujeet

    Wednesday, June 6, 2007 7:40 PM
  • Ha-ha, I knew it Smile

     

    It worries me that none of the samples use libraries. I have had parallel activities not working, breakpoints not being hit and now events don't work.

     

    Me thinks that some more complex samples are needed to weed out these "featues" Smile

     

    Cliff

     

    Friday, June 8, 2007 6:06 PM
  • I believe the issue you're seeing with wiring up events has to do with when you wire up. This is a workflow issue: the workflow status-change events, including Closed, are routed by activity name, rather than hooked up as normal .Net events. But for a custom activity, the name of the activity when it is constructed is different from the name when it is run (and has been named by the enclosing workflow).  For example, the event is looking for a handler on an activity called "Activity1", but the instance of Activity1 in the workflow is now called "activity11". The trick is to wire up the handler after the activity has been added to the main workflow. Tou could do this in the Initialize() override, or you could do it in a CodeActivity that runs at the beginning of the sequence.

     

    Dan

     

    Thursday, June 21, 2007 7:51 PM