728x90
반응형
스레드 생성법 : 일반
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | static void sf() { for (int i = 0; i < 10; i++) { Console.WriteLine("sf : {0}", i); Thread.Sleep(100); } } void f() { for (int i = 0; i < 10; i++) { Console.WriteLine("f : {0}", i); Thread.Sleep(100); } } static void Main(string[] args) { //일반적인 스레드 생성법 //메인이 끝까지 실행되더라도 할 일을 다하고 종료함 Thread t1 = new Thread(new ThreadStart(sf)); Thread t2 = new Thread(new ThreadStart(new MyProgram().f)); t1.Start(); //스레드를 작동시킴 t2.Start(); for (int i = 0; i < 2; i++) { Console.WriteLine("------------------------------------"); Thread.Sleep(100); } //t1.Join(); //t1 스레드가 작동 완료될 때까지 이 줄에서 계속 대기함 //t2.Join(); } | cs |
실행결과
스레드 생성법 : 스레드풀
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public static void Print1(object obj) { for (int i = 0; i < 10; i++) { Console.WriteLine("첫 번째 Thread :{0} ***", i); Thread.Sleep(100); } } public void Print2(object obj) { for (int i = 0; i < 10; i++) { Console.WriteLine("두 번째 Thread :{0} ***", i); Thread.Sleep(100); } } static void Main(string[] args) { //컨텍스트 스위칭이 많이 일어나서 느려질 것을 우려한다면 사용.. 일반적인 경우에는 사용하지 않음. //메인이 끝까지 실행되는 순간 같이 종료함 // static method를 이용한 ThreadPool 에 대한 작업 요청 ThreadPool.QueueUserWorkItem(new WaitCallback(Print1), null); // instance method를 이용한 ThreadPool 에 대한 작업 요청 ThreadPool.QueueUserWorkItem(new WaitCallback(new MyProgram().Print2), null); for (int i = 0; i < 2; i++) { Console.WriteLine("------------------------------------"); Thread.Sleep(100); } } | cs |
실행결과
스레드 생성법 : 타이머
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static void tPrint1(object obj) { Console.WriteLine("첫 번째 Thread : ***"); } public void tPrint2(object obj) { Console.WriteLine("두 번째 Thread : *******"); } static void Main(string[] args) { //타이머나 스레드는 시분할개념이기 때문에 타이머도 스레드와 유사한(?) 기능이라고 생각함. //함수(메인)가 끝까지 실행되는 순간 같이 종료함 Timer timer = new Timer(new TimerCallback(tPrint1),null,500,200); timer = new Timer(new TimerCallback((new MyProgram()).tPrint2),null,0,100); //timer.Dispose(); //Timer 를 종료 for (int i = 0; i < 2; i++) { Console.WriteLine("------------------------------------"); Thread.Sleep(500); } } | cs |
실행결과
현재 프로세스 정보 조회
1 2 3 4 5 6 7 8 9 10 | //현재 프로세스 정보 조회 Process proc = Process.GetCurrentProcess(); string processName = proc.ProcessName; DateTime startTime = proc.StartTime; int proID = proc.Id; int memory = proc.VirtualMemorySize; Console.WriteLine("******* 현재 프로세스 정보 *****"); Console.WriteLine(" Process: {0}\n ID: {1}\n 시작시간: {2}\n 메모리:{3}\n", processName, proID, startTime, memory); | cs |
실행결과
현재 프로세스에서 실행중인 스레드 목록 조회
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //현재 프로세스에서 실행중인 스레드 목록 조회 Process proc = Process.GetCurrentProcess(); ProcessThreadCollection ths = proc.Threads; int threadID; DateTime startTime; int priority; ThreadState thstate; int index = 1; Console.WriteLine(" 현재 프로세스에서 실행중인 스레드 수: " + ths.Count); foreach (ProcessThread pth in ths) { threadID = pth.Id; startTime = pth.StartTime; priority = pth.BasePriority; thstate = pth.ThreadState; Console.WriteLine("******* {0} 스레드 정보 *****", index++); Console.WriteLine(" ID: {0}\n 시작시간: {1}\n Priority: {2}\n 스레드 상태:{3}\n", threadID, startTime, priority, thstate); } | cs |
실행결과
스크롤이 길어질까봐 3개쯤에서 잘랐습니다
모든 프로세스 정보 조회
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //모든 프로세스 정보 조회 Process proc = Process.GetCurrentProcess(); string processName = proc.ProcessName; DateTime startTime = proc.StartTime; int proID = proc.Id; int memory = proc.VirtualMemorySize; Console.WriteLine("\n******* 모든 프로세스 정보 *****"); Process[] allProc = Process.GetProcesses(); Console.WriteLine("시스템에서 실행중인 프로세스의 수: {0}", allProc.Length); int index = 1; foreach (Process pro in allProc) { try { Console.WriteLine("***** {0}번째 프로세스 *****", index++); processName = pro.ProcessName; startTime = pro.StartTime; proID = pro.Id; memory = pro.VirtualMemorySize; Console.WriteLine(" Process: {0}\n ID: {1}\n 시작시간: {2}\n 메모리:{3}\n", processName, proID, startTime, memory); } catch { Console.WriteLine("#######################[{0}]#######################", pro.ProcessName); } } | cs |
실행결과
1~64번째 프로세스는 스크롤이 길어지므로 잘랐습니다
스레드 우선순위 설정법
1 2 3 4 5 6 7 8 | Thread th1 = new Thread(new ThreadStart(Print1)); Thread th2 = new Thread(new ThreadStart(Print2)); th1.Priority = ThreadPriority.Highest; th2.Priority = ThreadPriority.Lowest; th2.Start(); th1.Start(); | cs |
728x90
'코스웨어 > 15년 스마트컨트롤러' 카테고리의 다른 글
20150709 - 20번 엄민웅 [AVR]AX-12 (서브 모터제어) / [C#]채팅프로그램실습 (3) | 2015.07.09 |
---|---|
20150708 - 19번 안향진 AVR-AVR studio_AX-12+(스테핑모터) / C#-채팅프로그램 실습 / 네트워크-joinc 소켓프로그램 (2) | 2015.07.08 |
20150707 - 18번 안해운 AVR toolchain & C# attribute (4) | 2015.07.07 |
20150703 - 16번 박태인 - [C#]Thread 동기화, 파일 정보와 디렉토리 정보 다루기 (3) | 2015.07.03 |
20150701-13번-박서연 MSSQL연동&자동 업데이트 프로그램 (2) | 2015.07.01 |
20150626-11번-남수진 MySQL/클래스템플릿의 정적 멤버 변수 (6) | 2015.06.26 |
20150625 업무일지 출석번호 9번 김태현 (10) | 2015.06.25 |
20150624-8번-김태영-MySQL 비쥬얼스튜디오 연동, 클래스 템플릿 (6) | 2015.06.24 |