WorkflowInstance.EnqueueItemOnIdle Method
Posts a message to the specified workflow queue when the workflow is idle. EnqueueItemOnIdle waits until the workflow reaches an idle point and enqueues after verifying that the workflow scheduler is idle (that is, no active operation is being executed).
Assembly: System.Workflow.Runtime (in System.Workflow.Runtime.dll)
public void EnqueueItemOnIdle( IComparable queueName, Object item, IPendingWork pendingWork, Object workItem )
Parameters
- queueName
- Type: System.IComparable
The name of the WorkflowQueue.
- item
- Type: System.Object
The object to enqueue.
- pendingWork
- Type: System.Workflow.Runtime.IPendingWork
An IPendingWork that allows the sender to be notified when item is delivered.
- workItem
- Type: System.Object
An object to be passed to the IPendingWork methods.
| Exception | Condition |
|---|---|
| ArgumentNullException |
queueName is a null reference (Nothing in Visual Basic). |
| InvalidOperationException |
The workflow runtime engine is not running. -or- The workflow instance is suspended. -or- The WorkflowQueue specified by queueName does not exist. -or- The WorkflowQueue specified by queueName is not enabled. |
Waits for the workflow instance to become idle and then sends the item to the specified WorkflowQueue. If you call EnqueueItemOnIdle while the workflow instance is suspended, the workflow runtime engine will throw an InvalidOperationException. If you want to be notified when the message is delivered, you can implement IPendingWork in your service and pass a workItem and an IPendingWork object to EnqueueItem. If you do not want such notification, you can pass a null reference (Nothing in Visual Basic) for pendingWork and workItem.
When you are using this method with a state machine workflow, you might get an exception that contains the message "Queue '{0}' is not enabled." This occurs when the current state of the state machine does not know how to handle a specific event. For example, when some state other than the current state contains the EventDrivenActivity that contains the HandleExternalEventActivity that is represented by the queue '{0}'.
The following example demonstrates sending information to a workflow instance using the EnqueueItemOnIdle method. First, a WorkflowInstance object is created and started; next, an object that implements the IPendingWork interface is created. EnqueueItemOnIdle is then called, passing in string values for the queue name, the enqueued item, and the work item to be passed to the methods of the pendingWork object.
// Create a workflow runtime environment WorkflowRuntime workflowRuntime = new WorkflowRuntime(); // Create a new instance of the out-of-box SqlWorkflowPersistenceService. // Use the non-locking constructor, since we're only creating a single Workflow Runtime. NameValueCollection parameters = new NameValueCollection(); parameters.Add("ConnectionString", "Initial Catalog=SqlPersistenceService;Data Source=localhost;Integrated Security=SSPI;"); //Set UnloadOnIdle to true, so that the service will persist the workflow parameters.Add("UnloadOnIdle", "true"); SqlWorkflowPersistenceService persistenceService = new SqlWorkflowPersistenceService(parameters); // Add the service to the runtime workflowRuntime.AddService(persistenceService); // Create a WorkflowInstance object WorkflowInstance workflowInstance = workflowRuntime.CreateWorkflow(typeof(Workflow1)); // Start the instance workflowInstance.Start(); // Create an instance of a class that implements IPendingWork for notification PendingService pendingWork = new PendingService(); // Send the workflow the message workflowInstance.EnqueueItemOnIdle("ActionQueue", "StartWork", pendingWork, "ActionItem");
- Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see Using Libraries from Partially Trusted Code.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.