WorkflowInvoker.InvokeAsync 方法

定義

使用事件架構非同步設計模式,以非同步方式叫用工作流程。

多載

InvokeAsync(TimeSpan, Object)

使用指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。

InvokeAsync(IDictionary<String,Object>, TimeSpan)

使用指定的輸入參數 IDictionary<TKey,TValue> 和指定的逾時間隔,以非同步方式叫用工作流程。

InvokeAsync(IDictionary<String,Object>, Object)

使用指定的輸入參數 IDictionary<TKey,TValue> 和唯一識別碼,以非同步方式叫用工作流程。

InvokeAsync()

以非同步方式叫用工作流程。

InvokeAsync(Object)

使用指定的唯一識別碼,以非同步方式叫用工作流程。

InvokeAsync(IDictionary<String,Object>)

使用指定的輸入參數 IDictionary<TKey,TValue> 來非同步叫用工作流程。

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

使用指定的輸入參數 IDictionary<TKey,TValue>、指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。

InvokeAsync(TimeSpan)

使用指定的逾時間隔,以非同步方式叫用工作流程。

備註

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

InvokeAsync(TimeSpan, Object)

使用指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。

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 前完成的間隔。

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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 參數必須是唯一的。 如果 userState 不是唯一,則會擲回 ArgumentExceptionuserState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutException

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutException。 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

適用於

InvokeAsync(IDictionary<String,Object>, TimeSpan)

使用指定的輸入參數 IDictionary<TKey,TValue> 和指定的逾時間隔,以非同步方式叫用工作流程。

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>

工作流程的輸入參數字典,以引數名稱做為索引鍵。

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutException

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutException。 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

這個方法會儲存在工作中,它會傳回方法同步對應專案可以擲回的所有非使用狀況例外狀況。 如果例外狀況儲存在傳回的工作中,則會在等候工作時擲回該例外狀況。 使用狀況例外狀況,例如 ArgumentException,仍會同步擲回。 如需預存的例外狀況,請參閱 所 Invoke(IDictionary<String,Object>, TimeSpan)擲回的例外狀況。

適用於

InvokeAsync(IDictionary<String,Object>, Object)

使用指定的輸入參數 IDictionary<TKey,TValue> 和唯一識別碼,以非同步方式叫用工作流程。

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>

工作流程的輸入參數字典,以引數名稱做為索引鍵。

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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 參數必須是唯一的。 如果 userState 不是唯一,則會擲回 ArgumentExceptionuserState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

適用於

InvokeAsync()

以非同步方式叫用工作流程。

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

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

這個方法會儲存在工作中,它會傳回方法同步對應專案可以擲回的所有非使用狀況例外狀況。 如果例外狀況儲存在傳回的工作中,則會在等候工作時擲回該例外狀況。 使用狀況例外狀況,例如 ArgumentException,仍會同步擲回。 如需預存的例外狀況,請參閱 所 Invoke()擲回的例外狀況。

適用於

InvokeAsync(Object)

使用指定的唯一識別碼,以非同步方式叫用工作流程。

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

參數

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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 參數必須是唯一的。 如果 userState 參數不是唯一,則會擲回 ArgumentExceptionuserState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

適用於

InvokeAsync(IDictionary<String,Object>)

使用指定的輸入參數 IDictionary<TKey,TValue> 來非同步叫用工作流程。

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>

工作流程的輸入參數字典,以引數名稱做為索引鍵。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 若要設定工作流程必須完成的逾時間隔,請使用接受 InvokeAsync 的其中一個 TimeSpan 多載。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

這個方法會儲存在工作中,它會傳回方法同步對應專案可以擲回的所有非使用狀況例外狀況。 如果例外狀況儲存在傳回的工作中,則會在等候工作時擲回該例外狀況。 使用狀況例外狀況,例如 ArgumentException,仍會同步擲回。 如需預存的例外狀況,請參閱 所 Invoke(IDictionary<String,Object>)擲回的例外狀況。

適用於

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

使用指定的輸入參數 IDictionary<TKey,TValue>、指定的逾時間隔和唯一識別碼,以非同步方式叫用工作流程。

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>

工作流程的輸入參數字典,以引數名稱做為索引鍵。

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。

userState
Object

使用者提供的物件,可用來區別這個特定非同步叫用作業以及目前其他的非同步叫用作業。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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 參數必須是唯一的。 如果 userState 不是唯一,則會擲回 ArgumentExceptionuserState 會用來識別 InvokeCompleted 中的工作流程,並使用 CancelAsync 取消此工作流程。

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutException

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutException。 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

適用於

InvokeAsync(TimeSpan)

使用指定的逾時間隔,以非同步方式叫用工作流程。

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

參數

timeout
TimeSpan

工作流程必須在其中止並擲回 TimeoutException 前完成的間隔。

範例

下列範例會叫用由 LongRunningDiceRoll 活動組成的工作流程。 LongRunningDiceRoll 活動具有兩個輸出引數,這些引數代表擲骰作業的結果。 工作流程完成時,InvokeCompleted 處理常式就會擷取這些引數。

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.");

備註

若要在工作流程完成時收到通知,請處理 InvokeCompleted。 如果工作流程沒有在指定的逾時間隔內完成,則工作流程會中止並擲回 TimeoutException

注意

只有在超過逾時間隔及工作流程在執行期間變成閒置狀態時,才會擲回 TimeoutException。 需要比指定的逾時間隔還長的時間才能完成的工作流程,會在工作流程沒有變成閒置狀態時成功完成。

這個方法會使用事件架構非同步設計模式,以非同步方式叫用工作流程。 如需詳細資訊,請參閱事件架構非同步模式概觀

這個方法會儲存在工作中,它會傳回方法同步對應專案可以擲回的所有非使用狀況例外狀況。 如果例外狀況儲存在傳回的工作中,則會在等候工作時擲回該例外狀況。 使用狀況例外狀況,例如 ArgumentException,仍會同步擲回。 如需預存的例外狀況,請參閱 所 Invoke(TimeSpan)擲回的例外狀況。

適用於