Đang chuẩn bị liên kết để tải về tài liệu:
Apress-Visual CSharp 2010 Recipes A Problem Solution Approach_3

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Để hoàn thành một hoạt động không đồng bộ bắt đầu bằng cách sử dụng BeginInvoke, bạn gọi phương thức Control.EndInvoke. Các phương pháp BeginInvoke và EndInvoke tạo nên một mô hình không đồng bộ thực hiện phổ biến được biết đến với tên gọi là mô hình Async cổ điển. | CHAPTER 7 WINDOWS FORMS synchronously and BeginInvoke executes the delegate asynchronously. To complete an asynchronous operation initiated using BeginInvoke you call the Control.Endlnvoke 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 System.Action a delegate type that can encapsulate any method that returns void and takes no arguments. using System using System.Collections.Generic using System.ComponentModel using System.Data using System.Drawing using System.Text using System.Windows.Forms namespace Apress.VisualCSharpRecipes.Chapter07 public partial class Recipe07_19 Form Declare timers that change the button color. System.Timers.Timer greenTimer System.Timers.Timer redTimer public Recipe07_19 . . Initialization code is designer generated and contained in a separate file named Recipe07-19.Designer.cs. InitializeComponent Create autoreset timers that fire at varying intervals to change the color of the button on the form. greenTimer new System.Timers.Timer 3000 greenTimer.Elapsed new System.Timers.ElapsedEventHandler greenTimer_Elapsed greenTimer.Start redTimer new System.Timers.Timer 5000 redTimer.Elapsed new System.Timers.ElapsedEventHandler redTimer_Elapsed redTimer.Start 355 CHAPTER 7 WINDOWS FORMS void redTimer_Elapsed object sender System.Timers.ElapsedEventArgs e . Use an anonymous method to set the button color to red. button1.Invoke Action delegate button1.BackColor Color.Red void greenTimer_Elapsed object sender .

TÀI LIỆU LIÊN QUAN