Search code examples
workflow-foundation-4

While activity code condition in WF4


When the following is executed, the while loop never ends. I'm calling a method to get the value for the while loop condition here. Please tell me what I'm doing wrong?

    using System;
    using System.Linq;
    using System.Activities;
    using System.Activities.Statements;
    using System.IO;

   namespace BuildActivities
    {
   public sealed class CheckFile : CodeActivity
     {

    public InArgument<string> DirectoryName;

    protected override void Execute(CodeActivityContext context)
    {
        Activity workflow = new Sequence
        {
            Activities =
            {
                new While
            {

                Condition = GetValue() ,

                Body = new Sequence
                {
                    Activities = {
                        new WriteLine
                            {
                                Text = "Entered"
                            },
                        new WriteLine
                            {
                                Text = "Iterating"
                            },
                            new Delay
                            {
                Duration = System.TimeSpan.Parse("00:00:01")

                            }
                    }
                }

                //Duration = System.TimeSpan.Parse("00:00:01")
            },
            new WriteLine()
            {
                Text = "Exited"
            }
        }
        };
        try
        {
            WorkflowInvoker.Invoke(workflow, TimeSpan.FromSeconds(30));
        }
        catch (TimeoutException ex)
        {
            Console.WriteLine("The File still exist. Build Service has not picked up the file.");
        }
    }


    public bool GetValue()
    {
        bool matched = false;
        matched = File.Exists(@"\\vw189\release\buildservice\conshare.txt");
        return matched;
    }

}

}

when the code executes, i think it is only checking the while condition for one time. because, i have written some writeline to check how it works. and i see the loop never ends. I test this by deleting the file in the folder when the loop is running. There is a service which should pick the file every 5 sec. this is to determine whether that service is up and running or not.


Solution

  • Again, I don't understand what you're doing but having a workflow call inside a CodeActivity is wrong. I'll try to give you some options.

    Option 1:

    Having a CodeActivity which returns a boolean indicating if a file exists or not is the standard/correct way. Then you can use this activity on your workflow:

    public sealed class CheckFile : CodeActivity<bool>
    {
        public InArgument<string> FilePath { get; set; }
    
        protected override bool Execute(CodeActivityContext context)
        {
            return File.Exists(FilePath.Get(context));
        }
    }
    

    Options 2:

    Going along with your code, you would call File.Exists() through InvokeMethod:

    var workflow = new Sequence
    {
        Activities =
        {
            new While
            {
                Condition = new InvokeMethod<bool>
                {
                    TargetType = typeof (File),
                    MethodName = "Exists",
                    Parameters = { new InArgument<string>("c:\\file.txt") }
                },
                Body = new WriteLine {Text = "File still exists..."}
            },
            new WriteLine {Text = "File deleted."}
        }
    };
    

    PS: Your GetValue is called only once when workflow is build and evaluated just before being run by WorkflowInvoker. If you want it to be dynamic use activities like InvokeMethod like I showed you above. Again, don't try to use workflows just because, specially inside a CodeActivity.