次の方法で共有


While アクティビティでの中断のエミュレート

このトピックの内容は、Windows Workflow Foundation 4 に該当します。

このサンプルでは、DoWhileForEachWhile、および ParallelForEach の各アクティビティのループ機構を中断する方法を示します。

Windows Workflow Foundation (WF) にはこれらのループの実行を中断するアクティビティは用意されていないので、この方法が役立ちます。

シナリオ

このサンプルでは、ベンダー (Vendor クラスのインスタンス) のリストから信頼できるベンダーを探し、最初に見つかったベンダーを取得します。各ベンダーには、IDName、およびそのベンダーがどの程度信頼できるかを示す信頼度の数値があります。このサンプルでは、FindReliableVendor というカスタム アクティビティを作成します。このアクティビティは、2 つの入力パラメーター (ベンダーのリストと信頼度の最小値) を受け取り、リストのベンダーのうち、指定された条件に一致する最初のベンダーを返します。

ループの中断

Windows Workflow Foundation (WF) にはループを中断するアクティビティは用意されていません。コード サンプルでは、If アクティビティといくつかの変数を使用してループの中断を実現します。このサンプルでは、reliableVendor 変数に null 以外の値が代入されると、While アクティビティが中断されます。

while ループを中断する方法を示すコード例を次に示します。

// Iterates while the “i” variable is lower than the size of the list 
// and any reliable Vendor is found.      
new While(env => i.Get(env) < this.Vendors.Get(env).Count && reliableVendor.Get(env) == null)
{
    DisplayName = "Main loop. Breaks when a reliable vendor is found",
    Body = new Sequence
    {                            
        Activities =
        {
            // This is the if used for setting the value of the break value…
            new If
            {
                DisplayName = "Check for a reliable vendor",

                //  If a vendor satisfies the reliability level…
                Condition = new InArgument<bool>(env => 
                           this.Vendors.Get(env)[i.Get(env)].Reliability > 
                           this.MinimumReliability.Get(env)),

                // then assign that vendor to the reliable vendor variable and 
                // the while condition becomes false (exit the loop).
                Then = new Assign<Vendor>
                {
                    To = reliableVendor,
                    Value = new InArgument<Vendor>(env => 
                                  this.Vendors.Get(env)[i.Get(env)])
                }
            },

            // Increment the iteration variable. 
            new Assign<int>
            {
                DisplayName = "Increment iteration variable",
                To = i,
                Value = new InArgument<int>(env => i.Get(env) + 1)
            } 
        }
    }
}

このサンプルを使用するには

  1. Visual Studio 2010 を使用して、EmulatingBreakInWhile.sln ソリューション ファイルを開きます。

  2. ソリューションをビルドするには、F6 キーを押します。

  3. ソリューションを実行するには、Ctrl キーを押しながら F5 キーを押します。

Dd807393.Important(ja-jp,VS.100).gif 注 :
サンプルは、既にコンピューターにインストールされている場合があります。続行する前に、次の (既定の) ディレクトリを確認してください。

<InstallDrive>:\WF_WCF_Samples

このディレクトリが存在しない場合は、「.NET Framework 4 向けの Windows Communication Foundation (WCF) および Windows Workflow Foundation (WF) のサンプル」にアクセスして、Windows Communication Foundation (WCF) および WF のサンプルをすべてダウンロードしてください。このサンプルは、次のディレクトリに格納されます。

<InstallDrive>:\WF_WCF_Samples\WF\Basic\Built-InActivities\EmulatingBreakInWhile