![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
Visual C# 2010 Recipes solution_1
Số trang: 95
Loại file: pdf
Dung lượng: 1.87 MB
Lượt xem: 12
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_1, 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_1 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATIONusing System;using System.Threading;namespace Apress.VisualCSharpRecipes.Chapter04{ class Recipe04_03 { public static void Main() { // Create the state object that is passed to the TimerHandler // method when it is triggered. In this case, a message to display. string state = Timer expired.; Console.WriteLine({0} : Creating Timer., DateTime.Now.ToString(HH:mm:ss.ffff)); // Create a timer that fires first after 2 seconds and then every // second. Use an anonymous method for the timer expiry handler. using (Timer timer = new Timer(delegate(object s) {Console.WriteLine({0} : {1}, DateTime.Now.ToString(HH:mm:ss.ffff),s); } , state, 2000, 1000)) { int period; // Read the new timer interval from the console until the // user enters 0 (zero). Invalid values use a default value // of 0, which will stop the example. do { try { period = Int32.Parse(Console.ReadLine()); } catch (FormatException) { period = 0; } // Change the timer to fire using the new interval starting // immediately. if (period > 0) timer.Change(0, period); } while (period > 0); } 165 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATION // Wait to continue. Console.WriteLine(Main method complete. Press Enter.); Console.ReadLine(); } } } 4-4. Execute a Method at a Specific Time Problem You need to execute a method in a separate thread at a specific time. Solution Declare a method containing the code you want to execute. The method’s signature must match that defined by the System.Threading.TimerCallback delegate; that is, it must return void and take a single object argument. Create a System.Threading.Timer object, and pass it the method you want to execute along with a state object that the timer will pass to your method when the timer expires. Calculate the time difference between the current time and the desired execution time, and configure the Timer object to fire once after this period of time. How It Works Executing a method at a particular time is often useful. For example, you might need to back up data at 1 a.m. daily. Although primarily used for calling methods at regular intervals, the Timer object also provides the flexibility to call a method at a specific time. When you create a Timer object, you specify two time intervals. The first value specifies the millisecond delay until the Timer first executes your method. To execute the method at a specific time, you should set this value to the difference between the current time (System.DateTime.Now) and the desired execution time. The second value specifies the interval after which the Timer will repeatedly call your method following the initial execution. If you specify a value of 0, System.Threading.Timeout.Infinite, or TimeSpan(-1), the Timer object will execute the method only once. If you need the method to execute at a specific time every day, you can easily set this figure using TimeSpan.FromDays(1), which represents the number of milliseconds in 24 hours. The Code The following code demonstrates how to use a Timer object to execute a method at a specified time: using System; using System.Threading; using System.Globalization;166 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATIONnamespace Apress.VisualCSharpRecipes.Chapter04{ class Recipe04_04 { public static void Main(string[] args) { // Create a 30-second timespan. TimeSpan waitTime = new TimeSpan(0, 0, 30); // Create a Timer that fires once at the specified time. Specify // an interval of -1 to stop the timer executing the method // repeatedly. Use an anonymouse method for the timer expiry handler. new Timer(delegate(object s) { Console.WriteLine(Timer fired at {0}, ...
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_1 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATIONusing System;using System.Threading;namespace Apress.VisualCSharpRecipes.Chapter04{ class Recipe04_03 { public static void Main() { // Create the state object that is passed to the TimerHandler // method when it is triggered. In this case, a message to display. string state = Timer expired.; Console.WriteLine({0} : Creating Timer., DateTime.Now.ToString(HH:mm:ss.ffff)); // Create a timer that fires first after 2 seconds and then every // second. Use an anonymous method for the timer expiry handler. using (Timer timer = new Timer(delegate(object s) {Console.WriteLine({0} : {1}, DateTime.Now.ToString(HH:mm:ss.ffff),s); } , state, 2000, 1000)) { int period; // Read the new timer interval from the console until the // user enters 0 (zero). Invalid values use a default value // of 0, which will stop the example. do { try { period = Int32.Parse(Console.ReadLine()); } catch (FormatException) { period = 0; } // Change the timer to fire using the new interval starting // immediately. if (period > 0) timer.Change(0, period); } while (period > 0); } 165 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATION // Wait to continue. Console.WriteLine(Main method complete. Press Enter.); Console.ReadLine(); } } } 4-4. Execute a Method at a Specific Time Problem You need to execute a method in a separate thread at a specific time. Solution Declare a method containing the code you want to execute. The method’s signature must match that defined by the System.Threading.TimerCallback delegate; that is, it must return void and take a single object argument. Create a System.Threading.Timer object, and pass it the method you want to execute along with a state object that the timer will pass to your method when the timer expires. Calculate the time difference between the current time and the desired execution time, and configure the Timer object to fire once after this period of time. How It Works Executing a method at a particular time is often useful. For example, you might need to back up data at 1 a.m. daily. Although primarily used for calling methods at regular intervals, the Timer object also provides the flexibility to call a method at a specific time. When you create a Timer object, you specify two time intervals. The first value specifies the millisecond delay until the Timer first executes your method. To execute the method at a specific time, you should set this value to the difference between the current time (System.DateTime.Now) and the desired execution time. The second value specifies the interval after which the Timer will repeatedly call your method following the initial execution. If you specify a value of 0, System.Threading.Timeout.Infinite, or TimeSpan(-1), the Timer object will execute the method only once. If you need the method to execute at a specific time every day, you can easily set this figure using TimeSpan.FromDays(1), which represents the number of milliseconds in 24 hours. The Code The following code demonstrates how to use a Timer object to execute a method at a specified time: using System; using System.Threading; using System.Globalization;166 CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATIONnamespace Apress.VisualCSharpRecipes.Chapter04{ class Recipe04_04 { public static void Main(string[] args) { // Create a 30-second timespan. TimeSpan waitTime = new TimeSpan(0, 0, 30); // Create a Timer that fires once at the specified time. Specify // an interval of -1 to stop the timer executing the method // repeatedly. Use an anonymouse method for the timer expiry handler. new Timer(delegate(object s) { Console.WriteLine(Timer fired at {0}, ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhTài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 332 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 323 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 307 0 0 -
70 trang 267 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 249 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 234 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 227 0 0 -
Phần III: Xử lý sự cố Màn hình xanh
3 trang 222 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 216 0 0 -
Sao lưu dữ liệu Gmail sử dụng chế độ Offline
8 trang 213 0 0