共用方式為


WorkflowInstance.EnqueueItemOnIdle 方法

定義

工作流程閒置時,將訊息張貼至指定的工作流程佇列。 EnqueueItemOnIdle(IComparable, Object, IPendingWork, Object) 會等到工作流程到達閒置點,並在確認工作流程排程器閒置後 (也就是沒有任何正在執行的作用中操作時) 加入佇列中。

public:
 void EnqueueItemOnIdle(IComparable ^ queueName, System::Object ^ item, System::Workflow::Runtime::IPendingWork ^ pendingWork, System::Object ^ workItem);
public void EnqueueItemOnIdle (IComparable queueName, object item, System.Workflow.Runtime.IPendingWork pendingWork, object workItem);
member this.EnqueueItemOnIdle : IComparable * obj * System.Workflow.Runtime.IPendingWork * obj -> unit
Public Sub EnqueueItemOnIdle (queueName As IComparable, item As Object, pendingWork As IPendingWork, workItem As Object)

參數

queueName
IComparable

WorkflowQueue 的名稱。

item
Object

要加入佇列的物件。

pendingWork
IPendingWork

允許在傳遞 IPendingWork 時通知寄件者的 item

workItem
Object

要傳遞給 IPendingWork 方法的物件。

例外狀況

queueName 為 null 參考 (在 Visual Basic 中為 Nothing)。

工作流程執行階段引擎不在執行中。

-或- 工作流程執行個體已暫停。

-或- 由 WorkflowQueue 指定的 queueName 並不存在。

-或- WorkflowQueue 指定的 queueName 未啟用。

範例

下列程式碼範例將示範使用 EnqueueItemOnIdle 方法,將資訊傳送到工作流程執行個體。 首先會建立並啟動 WorkflowInstance 物件,接下來則會建立實作 IPendingWork 介面的物件。 EnqueueItemOnIdle 接著會呼叫 ,並傳入佇列名稱的字串值、排入佇列的專案,以及要傳遞至 pendingWork 物件方法的工作專案。

// 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");
' Create a workflow runtime environment
Dim workflowRuntime As 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.
Dim parameters As New NameValueCollection()
parameters.Add("ConnectionString", _
    "Initial Catalog=SqlPersistenceServiceData Source=localhostIntegrated Security=SSPI")
'Set UnloadOnIdle to true, so that the service will persist the workflow
parameters.Add("UnloadOnIdle", "true")
Dim persistenceService As _
   New SqlWorkflowPersistenceService(parameters)

' Add the service to the runtime
workflowRuntime.AddService(persistenceService)
' Create a WorkflowInstance object
Dim workflowInstance As WorkflowInstance = workflowRuntime.CreateWorkflow(GetType(Workflow1))
' Start the instance
workflowInstance.Start()
' Create an instance of a class that implements IPendingWork for notification
Dim pendingWork As New PendingService()
' Send the workflow the message
workflowInstance.EnqueueItemOnIdle("ActionQueue", "StartWork", pendingWork, "ActionItem")

備註

等待工作流程執行個體變成閒置,然後將 item 傳送到指定的 WorkflowQueue。 如果您在工作流程執行個體暫止時呼叫 EnqueueItemOnIdle,工作流程執行階段引擎將擲回 InvalidOperationException。 如果想要在傳遞訊息時得到通知,您可以在您的服務中實作 IPendingWork,並且傳遞 workItemIPendingWork 物件至 EnqueueItem。 如果不想要這類通知,您可以傳遞 NothingpendingWork 的 null 參考 (在 Visual Basic 中為 workItem)。

使用此方法搭配狀態機器工作流程時,您可能會取得帶有「未啟用佇列 '{0}'」訊息的例外狀況。 這種情況發生在狀態機器的目前狀態不知道如何處理特定事件時。 例如,當目前狀態以外的某種狀態包含 EventDrivenActivity,而且其中包含由佇列 '{0}' 所代表的 HandleExternalEventActivity 時。

適用於