Thread In C# Đa Luồng Trong C# Thread.Sleep Thread.Join Thread ...
Có thể bạn quan tâm
Quảng Cáo Đầu Bài
Translate
Menu List
- AAD1 (12)
- ADD2 (11)
- ADF2 (3)
- adsense (4)
- Ajax (5)
- Android (32)
- Angular 2 (59)
- AngularJS (9)
- Bảo Vật Quốc Gia (2)
- Blogger (4)
- Bootstrap 3 (2)
- C (7)
- C# (39)
- Canvas (1)
- Castles Abandoned (1)
- Chiến Tranh Thế Giới 2 (5)
- Chiến Tranh Việt Nam (12)
- Command Line (5)
- CSS (16)
- Deploy errors (1)
- Design pattern (8)
- Điện Tử Vi Mạch (47)
- Docker (1)
- Domain (1)
- eclipse (8)
- EJB (9)
- Excel (1)
- Facebook (1)
- Games pc (48)
- giaima (116)
- Git (5)
- Google (2)
- Hack (4)
- Horror Videos (1)
- Html (7)
- java (60)
- Java Advanced (14)
- Java Collection (3)
- Java Core (44)
- Java Interview Questions (10)
- JAVA Web Services (8)
- JavaFX (2)
- JavaScript (25)
- Job Search (1)
- jQuery (22)
- JSF (7)
- Json (7)
- Jsp & Servlet (45)
- Kiếm tiền online (8)
- Kiến Thức (1)
- Legendary and Feng Shui (2)
- Link Website (6)
- Linux (9)
- Management (2)
- Mobile (1)
- Mua Bán (1)
- MVC (1)
- Mysql (5)
- NetBeans (1)
- News (1)
- Nginx (1)
- NodeJS (27)
- QR Code (2)
- Radio Online (3)
- ReactJS (17)
- ReactJS Antd Design (1)
- ReactJS Notes (6)
- ReactJS Redux (12)
- Regex (1)
- RxJS (1)
- Shortcut (1)
- Software (1)
- spring (4)
- Spring Boot (4)
- Sql (23)
- Struts 1 Framework (8)
- Struts 2 Framework (27)
- Swing (39)
- Thủ thuật (1)
- tools (11)
- Tutorial (1)
- Vietnam War History (8)
- virus (2)
- Visual studio (1)
- Web (18)
- Wifi (2)
- Windows Store (47)
- World War (4)
- XML (26)
- Youtube (14)
Total Pageviews
Giai Ma. Powered by Blogger.07 June 2016
Thread in C# Đa Luồng trong C# Thread.Sleep Thread.Join Thread.Start CurrentThread
Property ThreadState và ThreadPriority
ThreadState Thuộc tính ThreadState cho thấy trạng thái hiện tại của thread. Mỗi một lời gọi phương thức của thread sẽ làm thay đổi giá trị thuộc tính này như Unstarted, Running, Suspended, Stopped, Aborted,…. ThreadPriority Thuộc tính này xác định mức độ ưu tiên mà thread sẽ được thực thi so với các thread khác. Mỗi thread khi được tạo ra mang giá trị priority là Normal. Các giá trị mà thuộc tính có thể có bao gồm: Lowest, BelowNormal, Normal, AboveNormal và Highest.Các phương thức thông dụng của Thread
– Abort(): khi phương thức này được gọi, hệ thống sẽ ném ra một ngoại lệThreadAbortException để kết thúc thread. Sau khi gọi phương thức này, thuộc tính ThreadState sẽ chuyển sang giá trị Stopped. – Suspend(): phương thức này sẽ tạm dừng việc thực thi của Thread vô thời hạn cho đến khi nó được yêu cầu chạy tiếp tục với phương thức Resume(). Tuy nhiên hai phương thức này được gắn attribute Obsolete để khuyến cáo rằng bạn nên sử dụng những phương pháp khác để thay thế. Các kĩ thuật này sẽ được giới thiệu trong một bài khác. – Sleep(): để dừng thread hiện tại trong một khoảng thời gian tính bằng milisecond, khi đó thread sẽ chuyển sang trạng thái WaitSleepJoin. Chú ý rằng đây là một phương thức static và bạn không cần tạo đối tượng Thread khi gọi nó, ví dụ: Thread.Sleep(1000). Tôi nhấn mạnh chữ hiện tại tức là tùy vào vị trí mà bạn gọi Thread.Sleep(), mà Thread thực thi dòng lệnh này sẽ bị ảnh hưởng. Nếu như bạn không tạo thêm Thread thì Thread đang thực thi chương trình sẽ bị ảnh hưởng (chương trình sẽ tạm ngừng hoạt động). Tạo đối tượng Thread và truyền một delegate ThreadStart chứa phương thức sẽ thực thi vào constructor của Thread. C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { //Tạo đối tượng ThreadStart bao lấy phương thức tĩnh(nó chỉ có thể bao lấy phương thức k tham số) //Tạo luồng Thread bao lấy ThreadStart Thread t = new Thread(new ThreadStart(MethodA)); t.Start(); MethodB(); Console.Read(); } static void MethodA() { for (int i = 0; i < 50; i++) { Console.Write("0"); } } static void MethodB() //Đây là phương tĩnh, không tham số { for (int i = 0; i < 50; i++) {Console.Write("1"); } } } } Nếu bạn không dùng thread chạy lần lượt 2 phương thức MethodA() và MethodB() thì kết quả in ra sẽ là 100 kí tự ‘0’ và sau đó là 100 kí tự ‘1’.
Truyền tham số cho Thread C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Student { public string Name { set; get; } public int Age { set; get; } } class Program { static void Main(string[] args) { Thread t = new Thread(Display); t.Start(new Student() { Name = "Admin", Age = 33}); Console.Read(); } static void Display(object o) { Student s = (Student)o; Console.Write(s.Name+" "+s.Age); } } } Output: Admin 33 Sử dụng phương thức không tĩnh và sử dụng Thread.Sleep() C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Worker { public string Name; public int Loop; public Worker(string name, int loop){ Name = name; Loop = loop; } public void DoWork() //Đây là phương thức không tĩnh, không tham số { for(int i =0; i< Loop; i++){ Console.WriteLine(Name +" Work "+Loop); Thread.Sleep(50); } } } class Program { static void Main(string[] args) { Worker w1 = new Worker("Admin",10); Thread t1 = new Thread(w1.DoWork); t1.Start(); Worker w2 = new Worker("\t\tSale", 10); Thread t2 = new Thread(w2.DoWork); t2.Start(); Console.Read(); } } }
Ví dự đơn giản dễ hiểu hơn về Sleep() C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { Thread t = new Thread(MethodA); t.Start(); MethodB(); Console.Read(); } static void MethodA() //Đây là một phương thức tĩnh, không tham số { Thread.Sleep(500); for (int i = 0; i < 100; i++ ) { Console.Write("-0-"); } } static void MethodB() { for (int i = 0; i < 100;i++ ) { Console.Write("1"); } } } }
ThreadStart C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Program { //Khai báo biến và tạo constructor có tham số private string Name; public Program(string name) { Name = name; } //Đây là phương thức không tĩnh, không tham số public void DoCode() { for (int i = 0; i < 5;i++ ) { Console.WriteLine(Name+" conffing..."); Thread.Sleep(50); } Console.Read(); } //Đây là phương thức tĩnh, không tham số public static void DoWork(){ for (int i = 0; i < 10;i++ ) { Console.WriteLine("*"); Thread.Sleep(100); } Console.Read(); } static void Main(string[] args) { //Vì DoWork là tĩnh nên không cần tạo đối tượng Program để gọi nó //Tạo đối tượng ThreadStart bao lấy phương thức tĩnh DoWork không tham số ThreadStart ts1 = new ThreadStart(DoWork); //Tạo đối tượng Thread bao lấy đối tượng Thread Thread t1 = new Thread(ts1); //Gọi start thread t1.Start(); //DoCode là không tĩnh nên cần tạo đối tượng Program để gọi nó Program p = new Program("Admin"); //Tạo đối tượng ThreadStart bao lấy phương thức không tĩnh DoCode ThreadStart ts2 = new ThreadStart(p.DoCode); //Tạo đối tượng Thread bao lấy ThreadStart Thread t2 = new Thread(ts2); //Gọi start thread t2.Start(); } } }
Thread - Delegate Hàm không có tên có tham số và không tham số Delegate C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { //Creat thread thực thi đoạn code Thread t1 = new Thread( delegate() //Delegate hàm không có tên { for (int i = 0; i < 10; i++) { Console.WriteLine("ThreadStart A " + i); Thread.Sleep(100); } } ); //Bắt đầu thread call threadt1.Start();//Creat thread thực thi đoạn code Thread t2 = new Thread( delegate(object value) //Delegate hàm không có tên nhưng có tham số { for (int i = 0; i < 10; i++) { Console.WriteLine("\t\t\t ThreadStart B {0} - {1}",i,value); Thread.Sleep(100); } } ); //Bắt đầu thread call thread t2.Start("Text"); Console.Read(); } } }
Đặt tên cho Thread (CurrentThread.Name) CurrentThread.Name C# 2016 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Threading; namespace Demo { class Program { static void Main(string[] args) { //Đặt tên cho luông chính Thread.CurrentThread.Name = "Main 01"; Console.WriteLine("Name thread: "+Thread.CurrentThread.Name); //Tạo một luồng bao lấy phương thức tĩnh Thread thread = new Thread(display); thread.Name = "\t\tMain 02"; thread.Start(); for (int i = 0; i < 10; i++) { Console.WriteLine("\tName: " + Thread.CurrentThread.Name); Thread.Sleep(60); } Console.Read(); } //Phương thức tĩnh không tham số public static void display() { for (int i = 0; i < 10;i++ ) { Console.WriteLine("\tName: " + Thread.CurrentThread.Name); Thread.Sleep(100); } } } }
← Newer Post Older Post → Home 0 nhận xét:
Post a Comment
Meditation Music , Relaxing Music
Bài đăng nổi bật
-
Video bocah berazil - Horror Videos Video bocah berazil "Warning: Can cause psychological trauma" (Video no sensor di bawah) Ceritanya : Ini adalah persaingan jual b... -
Google Gson API: Convert JSON String To ArrayList - Java Json String to ArrayList Java Json 2017 package com.gson.json; import java.lang.reflect.Type; import java.util.List; import com.go... -
Insert Update Delete Mysql Jsp & Servlet in Java NetBeans Database mysql http://localhost:8084/bai4/ index.jsp http://localhost:8084/bai4/ insert.jsp Khi ta nhấn insert button sẻ ... -
Hack Website Hack webs Hack một trang web bất kỳ Truy cập trang web cần tìm nhưng đáng ghét site đó lại yêu cầu phải đăng ký tài khoản của trang ... -
Mussolini Nhà độc tài nước Ý (Ông bạn vàng của Hitler ) và Người tình Petacci chết với khuôn mặt đáng sợ Thi thể nhà lãnh đạo Benito Mussolini (trái) cùng cô bồ Claratta Petacci sau bị quân du kích Italia hành quyết. Dù đã chết nhưng thi th...
Túy Hoa Âm & Nhất Tiễn Mai
Top site
- Angular-2-training
- Mergevideo
- Reactjs Router V5
- Addaudiotovideo
- Mp3tovideo
- Tunestotube
- Bootstrap Cheatsheet
- Photo Editor
- Download Video China Site
- React Usehooks
- React Hooks Core
- iCSS
- JS Interview
- Replicate AI
- Angular Blog
- Game PS3
- Kebab Case
- CSS Polygon
- CSS Design
- FE Tool
- MySQL
- Thủ Thuật FB
- Glitch
- Carbon
- SSL Config
- Server World
- Useweb3
- Git Branching
- Rapidapi
- Algorithm Visualizer
- JSV 9000
- Create Slide
- Giải Phương Trinh
- Grammar checker
- Benchmark framework
- Replicate explore
- Replicate animesr
Best Friend
BACK TO TOP
Từ khóa » Thread Trong C# Là Gì
-
C# – Cơ Bản Về Thread | YinYang's Programing Blog
-
Thread Và Tasks Trong C# Sử Dụng Như Thế Nào? - CodeLearn
-
Multi Threading (đa Luồng) Trong C# | How Kteam
-
Đa Luồng (Multithread) Trong C# - Học Lập Trình C# Online - VietTuts
-
Hướng Dẫn Lập Trình đa Luồng Trong C# - Openplanning
-
Tìm Hiểu C# Và ứng Dụng: Thread Và Sự đồng Bộ
-
Lập Trình Multi Thread C# Sử Dụng Parallel Chạy Song Song Các Tác Vụ
-
Thread Trong C# – Transinhs
-
Luồng Thực Thi (thread), Lập Trình đa Luồng (multithreading) | Tự Học ICT
-
Chi Tiết Blog Multithreading Trong Lập Trình - Vimentor
-
Thread Và Tasks Trong C# Sử Dụng Như Thế Nào? - Chickgolden
-
Lập Trình Bất đồng Bộ Trong C# - Viblo
-
Lock Statement - C# Reference | Microsoft Docs
-
Đa Luồng (Multithread) Trong C# - Vay Tiền Online Bằng CMND