Danh mục

Visual C# 2010 Recipes solution_3

Số trang: 95      Loại file: pdf      Dung lượng: 2.09 MB      Lượt xem: 14      Lượt tải: 0    
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu visual c# 2010 recipes solution_3, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_3 CHAPTER 7 ■ WINDOWS FORMSsynchronously and BeginInvoke executes the delegate asynchronously. To complete an asynchronousoperation initiated using BeginInvoke, you call the Control.EndInvoke method. The BeginInvoke andEndInvoke methods make up a common asynchronous execution pattern known as the Classic Asyncpattern. The details of this pattern and the options you have available for handling method completionare discussed in recipe 4-2.The CodeThe following example shows how to update a Windows Forms control from multiple threads. Theexample uses two timers that fire at differing intervals to change the color of a Button control betweenred and green. The code shows how to use both an anonymous method and a lambda expression withthe Invoke call. Both approaches use System.Action, a delegate type that can encapsulate any methodthat 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, System.Timers.ElapsedEventArgs e) { // Use a lambda expression to set the button color to green. button1.Invoke(new Action(() => button1.BackColor = Color.Green)); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } [STAThread] public static void Main(string[] args) { Application.Run(new Recipe07_19()); } } } 7-20. Display a Web Page in a Windows-Based Application Problem You want to display a web page and provide web-navigation capabilities within your Windows Forms application. Solution Use the WebBrowser control to display the web page and other standard controls like buttons and text boxes to allow the user to control the operation of the WebBrowser. ■ Caution The WebBrowser control is a managed wrapper around the WebBrowser ActiveX control. This means that you must ensure you annotate the Main method of your Windows application with the STAThread attribute and that you dispose of the WebBrowser control (by calling the WebBrowser.Dispose method) when it is no longer required.356 CHAPTER 7 ■ WINDOWS FORMSHow It WorksThe WebBrowser control makes it a trivial task to embed highly functional web browser capabilities intoyour Windows applications. The WebBrowser control is responsible for the display of web pages andmaintaining page history, but it does not provide any controls for user interaction. Instead, theWebBrowser control exposes properties and events that you can manipulate programmatically to controlthe operation of the WebBrowser. This approach makes the WebBrowser control highly flexible andadaptable to most common browsing requirements. Table 7-1 summarizes some of the WebBrowsermembers related to web navigation that you will find particularly useful.Table 7-1. Commonly Used Members of the WebBrowser ControlMember DescriptionP ropertyAllowNavigation Controls whether th ...

Tài liệu được xem nhiều: