locked
Flowchart workflow, Delay seems to hold thread RRS feed

  • Question

  • When I create a Flowchart workflow with a delay, and run it via WorkflowApplication.Run(), then try to terminate the main program thread, I would expect the workflow would terminate without a problem because the Delay has relinquished the ThreadPool thread.  But it does not.  If I don't cancel every running workflow, the application will not terminate.

    I should say that when my process ends, I just want the workflows to go away.  I don't need to persist them.  When I run the program again, I'll just start over.

    To demo this problem, I created a simple app.  I run a flowchart workflow that has a 10-second Delay, then a WriteLine("here") then loops back to the delay.  There's no way out of this loop.

    I know I can keep a list of all the WorkflowApplication instances, and loop through them when I stop.  But why do I need to?  What is keeping my process from terminating?  Is there a way to change this? 

    I invoke the workflow using this code: 

    class Program
    
    {
    
     static void Main(string[] args)
    
     {
    
      WorkflowApplication app = new WorkflowApplication(new Workflow1());
    
      app.Run();
    
      Console.WriteLine("<CR> to exit");
    
      Console.ReadLine();
    
      // Without this next line, the program will never terminate.
    
      app.Cancel(TimeSpan.FromSeconds(5));
    
     }
    
    }
    
    
    Wednesday, October 27, 2010 8:50 PM

Answers


  • I'm not very clear what you want to do.

    can you check whether this is what you want to do?       

     two ways for you

    1. single thread


                Activity root = new Sequence { Activities = {new Delay{Duration= new TimeSpan(0, 0, 10)} } };

                WorkflowInvoker.Invoke(root);
               
                Console.WriteLine("<CR> to exit");

                Console.ReadLine();

    2. multiple threads(if you are using WorkflowApplication, you need to call completed.Set() )

         Activity root = new Sequence { Activities = {new Delay{Duration= new TimeSpan(0, 0, 10)} } };
               
                AutoResetEvent completed = new AutoResetEvent(false);

                WorkflowApplication instance = new WorkflowApplication(root);

                instance.Completed = delegate(WorkflowApplicationCompletedEventArgs eArgs)
                {
                   //in a different thread
                    completed.Set();
                };

                instance.Run();

                Console.WriteLine("<CR> to exit");

                Console.ReadLine();

    Wednesday, October 27, 2010 11:36 PM

All replies


  • I'm not very clear what you want to do.

    can you check whether this is what you want to do?       

     two ways for you

    1. single thread


                Activity root = new Sequence { Activities = {new Delay{Duration= new TimeSpan(0, 0, 10)} } };

                WorkflowInvoker.Invoke(root);
               
                Console.WriteLine("<CR> to exit");

                Console.ReadLine();

    2. multiple threads(if you are using WorkflowApplication, you need to call completed.Set() )

         Activity root = new Sequence { Activities = {new Delay{Duration= new TimeSpan(0, 0, 10)} } };
               
                AutoResetEvent completed = new AutoResetEvent(false);

                WorkflowApplication instance = new WorkflowApplication(root);

                instance.Completed = delegate(WorkflowApplicationCompletedEventArgs eArgs)
                {
                   //in a different thread
                    completed.Set();
                };

                instance.Run();

                Console.WriteLine("<CR> to exit");

                Console.ReadLine();

    Wednesday, October 27, 2010 11:36 PM
  • My program is very much like your second example, except that I have defined the workflow through the workflow designer.

    Here's a real eye-popper I just discovered:  When I define the workflow using the designer (see my example), then run this program via Visual Studio (F5), the program will not terminate if a workflow is running.  If I run by double-clicking the .exe, or without the debugger (Ctrl-F5), the program will terminate as soon as I hit <Enter>.  Oddly, if I create the same workflow dynamically, as you did in your example, the F5 and Ctrl-F5 behavior is the same:  program terminates.

    So I guess it's just a thing that I'll see in development, and I can work around that.  Thanks for the help.

     

    Thursday, October 28, 2010 10:24 AM
  • I am seeing the same behavior. It would be nice to have a fix for this as it is screwing up a lot of tests.
    Tuesday, December 28, 2010 12:21 AM