tailieunhanh - Apress-Visual CSharp 2010 Recipes A Problem Solution Approach_5

synchronously and BeginInvoke executes the delegate asynchronously. To complete an asynchronous operation initiated using BeginInvoke, you call the method. The BeginInvoke and EndInvoke methods make up a common asynchronous execution pattern known as the Classic Async pattern. The details of this pattern and the options you have available for handling method completion are discussed in recipe 4-2. The Code The following example shows how to update a Windows Forms control from multiple threads. The example uses two timers that fire at differing intervals to change the color of a Button control between red and green. The code shows how to use. | CHAPTER 7 WINDOWS FORMS synchronously and BeginInvoke executes the delegate asynchronously. To complete an asynchronous operation initiated using BeginInvoke you call the method. The BeginInvoke and EndInvoke methods make up a common asynchronous execution pattern known as the Classic Async pattern. The details of this pattern and the options you have available for handling method completion are discussed in recipe 4-2. The Code The following example shows how to update a Windows Forms control from multiple threads. The example uses two timers that fire at differing intervals to change the color of a Button control between red and green. The code shows how to use both an anonymous method and a lambda expression with the Invoke call. Both approaches use a delegate type that can encapsulate any method that returns void and takes no arguments. using System using using using using using using namespace public partial class Recipe07_19 Form Declare timers that change the button color. greenTimer redTimer public Recipe07_19 . . Initialization code is designer generated and contained in a separate file named . InitializeComponent Create autoreset timers that fire at varying intervals to change the color of the button on the form. greenTimer new 3000 new greenTimer_Elapsed redTimer new 5000 new redTimer_Elapsed 355 CHAPTER 7 WINDOWS FORMS void redTimer_Elapsed object sender e . Use an anonymous method to set the button color to red. Action delegate void greenTimer_Elapsed object sender .

TỪ KHÓA LIÊN QUAN