QUESTION: What happens to a workflow instance I've created but never started?
ANSWER: Calling WorkflowRuntime.CreateWorkflow() will give you back a WorkflowInstance which, in essence, is a proxy to a runtime instance in the Created state. But what happens to that instance if it is never started?
Upon calling CreateWorkflow you have caused the runtime to create an in-memory instance of the workflow. This includes calling Initialize on the entire workflow tree as well as readying runtime resources like tracking channels. This in-memory instance will not "unload on idle" because the Created state is technically not idle ... only something that is running can become idle.
That means that the in-memory instance will remain in memory, consuming resources, until one of the following happens:
- WorkflowInstance.Start is called - If the workflow is started then it will begin to process and, as such, is subject to whatever unloading policy the host has implemented.
- WorkflowInstance.Abort is called - Abort causes the in-memory instance to be removed without any side effects on persistence. Therefore, if you have not forced your Created workflow to persist by calling Unload, you can safely call Abort to free up the resources and return to the same state as if you had never created the instance in the first place.
- WorkflowInstance.Unload is called - This will cause the Created instance to unload to the persistence service. The in-memory instance will be destroyed. Note that an unloaded, Created workflow will NOT start running when the runtime is started using our out of box SqlWorkflowPersistenceService. Only unblocked Started workflows will automatically continue processing at startup.
- WorkflowInstance.Terminate is called - This will cause the in-memory instance to be destroyed and the persistence service to be notified that the instance has been terminated. If the Created instance was never unloaded, then this will have the same net effect as Abort, but if it had been unloaded then Terminate should be called if you really want to get rid of the unloaded instance.
- WorkflowRuntime.Stop is called - When the WorkflowRuntime is stopped all the workflows are unloaded. That means that the Created workflow instance will be persisted just as if WorkflowInstance.Unload had been called.
- The process is exited without WorkflowRuntime.Stop being called - This is the same as Abort. If the instance was never persisted then the in-memory version is lost and it was as though the instance were never created. If the instance had been persisted then it can be retrieved and started at a later time.
In short, if you've created a workflow instance which you don't want to consume any more resources, call Terminate on it. This will make sure that it is taken out of memory as well as cleaned up from any persistence service.