프로세스
NUM.TXT에 저장되어 있는 5 쌍의 숫자들을 add_2sec.exe 를 통해 덧셈을 실행시킨 후 각각의 결과들을 모두 출력하시오
* add_2sec.exe [num1] [num2]실행하면 , 2 초 후 num1 num2 의 값을 출력함
[입력 ] NUM.TXT
1 2
3 4
5 6
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; namespace ProcessExe { class Program { static string getProcessOutput(string fileName, string args) { ProcessStartInfo start = new ProcessStartInfo(); start.FileName = fileName; start.UseShellExecute = false; start.RedirectStandardOutput = true; start.CreateNoWindow = true; start.Arguments = args; // argument 전달 Process process = Process.Start(start); // process를 실행시키고 StreamReader reader = process.StandardOutput; // 출력되는 값을 가져오기 위해 StreamReader에 연결 return reader.ReadLine(); // 출력값의 한 라인을 읽는다 } static void doWork(Object obj) { Tuple<int, int> nums = (Tuple<int, int>)obj; string args = nums.Item1.ToString() + " " + nums.Item2.ToString(); string sum = getProcessOutput("add_2sec.exe", args); Console.WriteLine(nums.Item1 + " + " + nums.Item2 + " = " + sum); } static void Main(string[] args) { Console.WriteLine("Start - " + DateTime.Now); List<Thread> thList = new List<Thread>(); StreamReader sr = new StreamReader("NUM.TXT"); string line; while ((line = sr.ReadLine()) != null) { string[] words = line.Split(' '); int num1 = int.Parse(words[0]); int num2 = int.Parse(words[1]); Thread th = new Thread(doWork); Object obj = (Object)new Tuple<int, int>(num1, num2); th.Start(obj); thList.Add(th); } sr.Close(); foreach (var th in thList) { th.Join(); } Console.WriteLine("End - " + DateTime.Now); } } } |
쓰레드
Thread 2개를 만든 후 , Main 함수와 Thread 2 개에서 동시에 0 부터 9 까지 출력하시오 .
어디서 출력하였는지 구분할 수 있게 숫자 앞에 [Main], [Thread1], [Thread2] 표시하시오
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Systehttp://m.Threading.Tasks; using System.Threading; namespace ThreadTest { class ThreadSample { public class Worker { // This method will be called when the thread is started. public void DoWork() { for (int i=0; i<10; i++) { Console.WriteLine("[Thread"+num+"] "+i); Thread.Sleep(1); } } public int num; } static void Main(string[] args) { // Create the thread object. This does not start the thread. Worker workerObject1 = new Worker(); workerObject1.num = 1; Thread workerThread1 = new Thread(workerObject1.DoWork); Worker workerObject2 = new Worker(); workerObject2.num = 2; Thread workerThread2 = new Thread(workerObject2.DoWork); // Start the worker thread. workerThread1.Start(); workerThread2.Start(); for (int i = 0; i < 10; i++) { Console.WriteLine("[Main] "+i); // Put the main thread to sleep for 1 millisecond to // allow the worker thread to do some work: Thread.Sleep(1); } // Use the Join method to block the current thread // until the object's thread terminates. workerThread1.Join(); workerThread2.Join(); } } } |
'프로그래밍 > C#' 카테고리의 다른 글
Json 2 (0) | 2024.05.13 |
---|---|
Json (0) | 2024.05.13 |
입력 메시지들을 저장하는 Queue 작성 (0) | 2024.05.13 |
Map을 이용하여 회사원의 투입플젝 확인 하기 (0) | 2024.05.13 |
학생 성적표를 List로 오름차순 내림차순 표시 (0) | 2024.05.13 |