C# 2005 Programmer’s Reference - chapter 23

Tham khảo tài liệu 'c# 2005 programmer’s reference - chapter 23', 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ả | 514 CHAPTER 23 Threads Asynchronous and Parallel Programming Exiting WriteLock on thread 0 id 1000 static void ReadThread object state int id for int i 0 i MaxValues i Entered ReadLock on thread 0 id StringBuilder sb new StringBuilder for int j 0 j i j if 0 _array j Array 0 on thread 1 sb id Exiting ReadLock on thread 0 id 1000 Here s a portion of the output Entered WriteLock on thread 7 Added 25 to array on thread 7 Exiting WriteLock on thread 7 Entered ReadLock on thread 11 Entered ReadLock on thread 12 Array 0 1 4 9 on thread 12 Exiting ReadLock on thread 12 Array 0 1 4 9 16 on thread 11 Exiting ReadLock on thread 11 Entered ReadLock on thread 13 Array 0 1 4 9 on thread 13 Exiting ReadLock on thread 13 Notice that threads 11 and 12 were able to get a read lock at the same time. Use the Asynchronous Programming Model 515 Use the Asynchronous Programming Model Scenario Problem You have many actions in your application than can be accomplished in the background. On many I O classes you will find a common set of methods. For example on streams you can call Read or BeginRead. The first is a synchronous method that does not return until the data has been read. The second returns immediately letting your program continue while the file is read from disk the network the Internet or another data source. Solution The TextTokenizer sample app shown in Figure demonstrates how to use this method of programming. If only synchronous methods were used it would be impossible to interact with the program even just to close it but using asynchronous methods allows your program to respond to users and perform other tasks while the I O is completing. FIGURE The TextTokenizer app demonstrates asynchronous file reading as well as implementing asynchronous

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
80    441    10    30-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.