Click here to Skip to main content
15,797,822 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hello I am new to Windows Workflow foundation and try to write workflow (State Machine ) using Object Model. I am Getting This error

StateMachine "Activity1" Must have an initial state .

Can any body please give me answer . My code is below...Thanks in advance

C#
Activity SMwf = new StateMachine()
           {

               States =
               {
                  new State(){
                     Entry = new Sequence()
                     {
                         Activities = {

                             new WriteLine(){Text="Entry Point....."},
                             new Delay(){ Duration = TimeSpan.FromSeconds(30)}
                         }
                     },
                     Exit = new Sequence(){
                           Activities = {

                               new WriteLine(){Text = "Exit Point"},
                               new Delay(){ Duration = TimeSpan.FromSeconds(30)}
                           }
                     },
                     Transitions = {
                         new Transition {
                         DisplayName ="T1",
                         Action ={},
                         Condition={
                         },

                         }

                     },
                     DisplayName ="State1",

                  },
                  new State(){
                     Entry = new Sequence()
                     {
                         Activities = {

                             new WriteLine(){Text="Entry Point....."},
                             new Delay(){ Duration = TimeSpan.FromSeconds(30)}
                         }
                     },
                     Exit = new Sequence(){
                           Activities = {

                               new WriteLine(){Text = "Exit Point"},
                               new Delay(){ Duration = TimeSpan.FromSeconds(30)}
                           }
                     },
                     DisplayName ="State1",

                  }
               },
               DisplayName = "Activity1",


           };

           try
           {
               WorkflowInvoker.Invoke(SMwf);
           }
           catch (Exception ex)
           {
               Console.WriteLine(ex.Message);
           }
Posted

You need to set InitialState[^] to the particular state that should start off the processing. You could set it before you call WorkflowInvker.Invoke(SMwf); like this:
C#
StateMachine sm = (StateMachine)SMwf;
sm.InitialState = sm.States[0];
Better still, don't do the cast - create SMwf as a StateMachine instead of an Activity.
 
Share this answer
 
Thanks But Now it gives error that Activity1 must have atleast one Transition
 
Share this answer
 
v2
Comments
Pete O'Hanlon 22-Jan-14 9:21am    
How can you have accepted your own answer? It makes no sense when you are asking another question. If you want to reply to someone, click on the Have a Question or Comment? button below their answer. You're lucky I decided to revisit this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900