Danh mục

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    
Jamona

Hỗ trợ phí lưu trữ khi tải xuống: 23,000 VND Tải xuống file đầy đủ (95 trang) 0

Báo xấu

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}, ...

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