使用 Pick 活動Using the Pick Activity

這個範例示範如何使用 Pick 活動。This sample demonstrates how to use the Pick activity.

Pick 活動會提供以事件為主的控制模型。The Pick activity provides event-based control modeling. 其行為與 C# switch 陳述式類似,只執行 switch 陳述式的其中一個分支。It behaves similar to the C# switch statement, which executes only one of the branches in the switch statement. 不同於 switch 陳述式是根據值來執行分支,Pick 活動是根據活動完成方式來執行分支。Unlike the switch statement in which a branch is executed based upon on a value, the Pick activity executes a branch based upon how an activity completes.

這個範例會提示使用者在主控台於給定時限內輸入名稱。This sample prompts a user to type in their name on the console within a given time period. 範例中的 Pick 活動有兩個分支,它們是根據使用者是否在 5 秒內輸入名稱來執行。The Pick activity in the sample has two branches that are executed based upon whether the user types in their name within 5 seconds or not. 如果使用者在 5 秒內輸入名稱,則會執行包含自訂 ReadLine 活動的第一個分支,否則會執行包含 Delay 活動的另一個分支。If the user types in their name within 5 seconds, the first branch is executed, which contains a custom ReadLine activity; otherwise the other branch is executed, which contains a Delay activity. 在主控台輸入使用者名稱後,就會在主控台上列印此名稱。Once a user’s name is typed in on the console, the user’s name is printed on the console. 如果沒有在 5 秒內輸入,則作業逾時。If an input is not entered within 5 seconds, the operation is timed out.

示範Demonstrates

Pick 活動。Pick activity.

討論Discussion

此範例包含設計工具工作流程和程式碼工作流程。The sample includes a Designer workflow and coded workflow.

設計工具工作流程範例的設計工具版本會示範如何在設計工具中建立工作流程。Designer Workflow The Designer version of the sample demonstrates how to create a workflow in the designer. 包含下列檔案:The following files are included:

  • Program.cs:包含執行範例工作流程的 Main 函數。Program.cs : Includes the Main function that executes the sample workflow.

  • ReadString.cs:從主控台讀取輸入的自訂活動。ReadString.cs: A custom activity that reads some input from the console.

  • Sequence1.xaml:在使用 Pick 的設計工具中建立工作流程。Sequence1.xaml: A workflow created using the designer that uses Pick.

自動程式碼工作流程範例的程式碼版本示範如何在設計工具中建立工作流程。Coded Workflow The coded version of the sample demonstrates how to create a workflow in the designer. 包含下列檔案:The following files are included:

  • Program.cs:包含執行範例工作流程的 Main 函數。Program.cs : Includes the Main function that executes the sample workflow.

  • ReadString.cs:從主控台讀取輸入的自訂活動。ReadString.cs: A custom activity that reads some input from the console.

若要使用這個範例To use this sample

  1. 使用 Visual Studio 2010,開啟 [挑選 .sln 方案檔]。Using Visual Studio 2010, open the Pick.sln solution file.

  2. 若要建置此方案,請按 CTRL+SHIFT+B。To build the solution, press CTRL+SHIFT+B.

  3. 若要執行此方案,請按 F5。To run the solution, press F5.

重要

這些範例可能已安裝在您的電腦上。The samples may already be installed on your machine. 請先檢查下列 (預設) 目錄,然後再繼續。Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

如果此目錄不存在,請移至.NET Framework 4 的 Windows Communication Foundation (wcf)和 Windows Workflow Foundation (WF)範例,以下載所有 WINDOWS COMMUNICATION FOUNDATION (wcf)和 WFWF 範例。If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WFWF samples. 此範例位於下列目錄。This sample is located in the following directory.

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