WorkflowInvoker.InvokeAsync 方法

定義

使用事件架構非同步設計模式,以非同步方式叫用工作流程。Invokes a workflow asynchronously using the event-based asynchronous design pattern.

多載

InvokeAsync(TimeSpan, Object)

使用指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified time-out interval and a unique identifier.

InvokeAsync(IDictionary<String,Object>, TimeSpan)

使用指定的輸入參數 IDictionary<TKey,TValue> 和指定的逾時間隔,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified IDictionary<TKey,TValue> of input parameters and the specified time-out interval.

InvokeAsync(IDictionary<String,Object>, Object)

使用指定的輸入參數 IDictionary<TKey,TValue> 和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously using the specified IDictionary<TKey,TValue> of input parameters and a unique identifier.

InvokeAsync()

以非同步方式叫用工作流程。Invokes a workflow asynchronously.

InvokeAsync(Object)

使用指定的唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously using the specified unique identifier.

InvokeAsync(IDictionary<String,Object>)

使用指定的輸入參數 IDictionary<TKey,TValue> 來非同步叫用工作流程。Invokes a workflow asynchronously using the specified IDictionary<TKey,TValue> of input parameters.

InvokeAsync(IDictionary<String,Object>, TimeSpan, Object)

使用指定的輸入參數 IDictionary<TKey,TValue>、指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified IDictionary<TKey,TValue> of input parameters, the specified time-out interval, and a unique identifier.

InvokeAsync(TimeSpan)

使用指定的逾時間隔,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified time-out interval.

備註

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(TimeSpan, Object)

使用指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified time-out interval and a unique identifier.

public:
 void InvokeAsync(TimeSpan timeout, System::Object ^ userState);
public void InvokeAsync (TimeSpan timeout, object userState);
member this.InvokeAsync : TimeSpan * obj -> unit
Public Sub InvokeAsync (timeout As TimeSpan, userState As Object)

參數

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。The interval in which the workflow must complete before it is aborted and a TimeoutException is thrown.

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。A user-provided object used to distinguish this particular asynchronous invoke operation from other current asynchronous invoke operations.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

在目前活動的所有執行中userState 作業裡,InvokeAsync 參數必須是唯一的。The userState parameter must be unique across all currently running InvokeAsync operations for the current activity. 如果 userState 不是唯一,則會擲回 ArgumentExceptionIf userState is not unique, an ArgumentException is thrown. userState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。userState is used to identify the workflow in InvokeCompleted, and to cancel the workflow using CancelAsync.

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutExceptionIf the workflow does not complete within the specified time-out interval the workflow is aborted and a TimeoutException is thrown.

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutExceptionThe TimeoutException is only thrown if the time-out interval elapses and the workflow becomes idle during execution. 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。A workflow that takes longer than the specified time-out interval to complete completes successfully if the workflow does not become idle.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(IDictionary<String,Object>, TimeSpan)

使用指定的輸入參數 IDictionary<TKey,TValue> 和指定的逾時間隔,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified IDictionary<TKey,TValue> of input parameters and the specified time-out interval.

public:
 void InvokeAsync(System::Collections::Generic::IDictionary<System::String ^, System::Object ^> ^ inputs, TimeSpan timeout);
public void InvokeAsync (System.Collections.Generic.IDictionary<string,object> inputs, TimeSpan timeout);
member this.InvokeAsync : System.Collections.Generic.IDictionary<string, obj> * TimeSpan -> unit
Public Sub InvokeAsync (inputs As IDictionary(Of String, Object), timeout As TimeSpan)

參數

inputs
IDictionary<String,Object>

工作流程的輸入參數字典,以引數名稱做為索引鍵。The dictionary of input parameters to the workflow, keyed by argument name.

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。The interval in which the workflow must complete before it is aborted and a TimeoutException is thrown.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutExceptionIf the workflow does not complete within the specified time-out interval the workflow is aborted and a TimeoutException is thrown.

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutExceptionThe TimeoutException is only thrown if the time-out interval elapses and the workflow becomes idle during execution. 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。A workflow that takes longer than the specified time-out interval to complete completes successfully if the workflow does not become idle.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(IDictionary<String,Object>, Object)

使用指定的輸入參數 IDictionary<TKey,TValue> 和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously using the specified IDictionary<TKey,TValue> of input parameters and a unique identifier.

public:
 void InvokeAsync(System::Collections::Generic::IDictionary<System::String ^, System::Object ^> ^ inputs, System::Object ^ userState);
public void InvokeAsync (System.Collections.Generic.IDictionary<string,object> inputs, object userState);
member this.InvokeAsync : System.Collections.Generic.IDictionary<string, obj> * obj -> unit
Public Sub InvokeAsync (inputs As IDictionary(Of String, Object), userState As Object)

參數

inputs
IDictionary<String,Object>

工作流程的輸入參數字典,以引數名稱做為索引鍵。The dictionary of input parameters to the workflow, keyed by argument name.

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。A user-provided object used to distinguish this particular asynchronous invoke operation from other current asynchronous invoke operations.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

在目前活動的所有執行中userState 作業裡,InvokeAsync 參數必須是唯一的。The userState parameter must be unique across all currently running InvokeAsync operations for the current activity. 如果 userState 不是唯一,則會擲回 ArgumentExceptionIf userState is not unique, an ArgumentException is thrown. userState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。userState is used to identify the workflow in InvokeCompleted, and to cancel the workflow using CancelAsync.

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync()

以非同步方式叫用工作流程。Invokes a workflow asynchronously.

public:
 void InvokeAsync();
public void InvokeAsync ();
member this.InvokeAsync : unit -> unit
Public Sub InvokeAsync ()

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(Object)

使用指定的唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously using the specified unique identifier.

public:
 void InvokeAsync(System::Object ^ userState);
public void InvokeAsync (object userState);
member this.InvokeAsync : obj -> unit
Public Sub InvokeAsync (userState As Object)

參數

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。A user-provided object used to distinguish this particular asynchronous invoke operation from other current asynchronous invoke operations.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

在目前活動的所有執行中userState 作業裡,InvokeAsync 參數必須是唯一的。The userState parameter must be unique across all currently running InvokeAsync operations for the current activity. 如果 userState 參數不是唯一,則會擲回 ArgumentExceptionIf the userState parameter is not unique, an ArgumentException is thrown. userState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。userState is used to identify the workflow in InvokeCompleted, and to cancel the workflow using CancelAsync.

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(IDictionary<String,Object>)

使用指定的輸入參數 IDictionary<TKey,TValue> 來非同步叫用工作流程。Invokes a workflow asynchronously using the specified IDictionary<TKey,TValue> of input parameters.

public:
 void InvokeAsync(System::Collections::Generic::IDictionary<System::String ^, System::Object ^> ^ inputs);
public void InvokeAsync (System.Collections.Generic.IDictionary<string,object> inputs);
member this.InvokeAsync : System.Collections.Generic.IDictionary<string, obj> -> unit
Public Sub InvokeAsync (inputs As IDictionary(Of String, Object))

參數

inputs
IDictionary<String,Object>

工作流程的輸入參數字典,以引數名稱做為索引鍵。The dictionary of input parameters to the workflow, keyed by argument name.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。To configure a time-out interval in which the workflow must complete, use one of the InvokeAsync overloads that take a TimeSpan.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(IDictionary<String,Object>, TimeSpan, Object)

使用指定的輸入參數 IDictionary<TKey,TValue>、指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified IDictionary<TKey,TValue> of input parameters, the specified time-out interval, and a unique identifier.

public:
 void InvokeAsync(System::Collections::Generic::IDictionary<System::String ^, System::Object ^> ^ inputs, TimeSpan timeout, System::Object ^ userState);
public void InvokeAsync (System.Collections.Generic.IDictionary<string,object> inputs, TimeSpan timeout, object userState);
member this.InvokeAsync : System.Collections.Generic.IDictionary<string, obj> * TimeSpan * obj -> unit
Public Sub InvokeAsync (inputs As IDictionary(Of String, Object), timeout As TimeSpan, userState As Object)

參數

inputs
IDictionary<String,Object>

工作流程的輸入參數字典,以引數名稱做為索引鍵。The dictionary of input parameters to the workflow, keyed by argument name.

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。The interval in which the workflow must complete before it is aborted and a TimeoutException is thrown.

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。A user-provided object used to distinguish this particular asynchronous invoke operation from other current asynchronous invoke operations.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

在目前活動的所有執行中userState 作業裡,InvokeAsync 參數必須是唯一的。The userState parameter must be unique across all currently running InvokeAsync operations for the current activity. 如果 userState 不是唯一,則會擲回 ArgumentExceptionIf userState is not unique, an ArgumentException is thrown. userState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。userState is used to identify the workflow in InvokeCompleted, and to cancel the workflow using CancelAsync.

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutExceptionIf the workflow does not complete within the specified time-out interval the workflow is aborted and a TimeoutException is thrown.

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutExceptionThe TimeoutException is only thrown if the time-out interval elapses and the workflow becomes idle during execution. 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。A workflow that takes longer than the specified time-out interval to complete completes successfully if the workflow does not become idle.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

InvokeAsync(TimeSpan)

使用指定的逾時間隔,以非同步方式叫用工作流程。Invokes a workflow asynchronously with the specified time-out interval.

public:
 void InvokeAsync(TimeSpan timeout);
public void InvokeAsync (TimeSpan timeout);
member this.InvokeAsync : TimeSpan -> unit
Public Sub InvokeAsync (timeout As TimeSpan)

參數

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。The interval in which the workflow must complete before it is aborted and a TimeoutException is thrown.

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。The following example invokes a workflow consisting of a LongRunningDiceRoll activity. LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。The LongRunningDiceRoll activity has two output arguments that represent the results of the dice roll operation. 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。When the workflow completes these are retrieved in the InvokeCompleted handler.

public sealed class LongRunningDiceRoll : Activity
{
    public OutArgument<int> D1 { get; set; }
    public OutArgument<int> D2 { get; set; }

    public LongRunningDiceRoll()
    {
        this.Implementation = () => new Sequence
        {
            Activities =
            {
                new WriteLine
                {
                    Text = "Rolling the dice for 5 seconds."
                },
                new Delay
                {
                    Duration = TimeSpan.FromSeconds(5)
                },
                new DiceRoll
                {
                    D1 = new OutArgument<int>(env => this.D1.Get(env)),
                    D2 = new OutArgument<int>(env => this.D2.Get(env))
                }
            }
        };
    }
}
AutoResetEvent syncEvent = new AutoResetEvent(false);

WorkflowInvoker invoker = new WorkflowInvoker(new LongRunningDiceRoll());

invoker.InvokeCompleted += delegate(object sender, InvokeCompletedEventArgs args)
{
    if (args.Cancelled == true)
    {
        Console.WriteLine("Workflow was cancelled.");
    }
    else if (args.Error != null)
    {
        Console.WriteLine("Exception: {0}\n{1}",
            args.Error.GetType().FullName,
            args.Error.Message);
    }
    else
    {
        Console.WriteLine("The two dice are {0} and {1}.",
            args.Outputs["D1"], args.Outputs["D2"]);
    }

    syncEvent.Set();
};

invoker.InvokeAsync("InvokeAsync Example");

Console.WriteLine("Waiting for the workflow to complete.");

// Wait for the workflow to complete.
syncEvent.WaitOne();

Console.WriteLine("The workflow is complete.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompletedTo be notified when the workflow is complete, handle InvokeCompleted. 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutExceptionIf the workflow does not complete within the specified time-out interval the workflow is aborted and a TimeoutException is thrown.

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutExceptionThe TimeoutException is only thrown if the time-out interval elapses and the workflow becomes idle during execution. 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。A workflow that takes longer than the specified time-out interval to complete completes successfully if the workflow does not become idle.

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。This method invokes a workflow asynchronously using the event-based asynchronous design pattern. 如需詳細資訊,請參閱事件架構非同步模式概觀For more information, see Event-based Asynchronous Pattern Overview.

適用於