본문 바로가기
300x250

C#/책 예제16

C# Task 비동기 호출의 병렬 처리 하기 비동기 호출의 병렬 처리 하기 입니다. 아래의 예제는 병렬 처리 하기 전입니다. using System; using System.IO; using System.Threading; using System.Threading.Tasks; namespace TaskSample { class Program { static void Main(string[] args) { int result3 = Method3(); int result5 = Method5(); Console.WriteLine(result3 + result5); } private static int Method3() { Thread.Sleep(3000); // 3초가 걸리는 작업을 대신해서 sleep 처리 return 3; } private stati.. 2019. 6. 9.
C# Task ReadAllText 메서드를 비동기로 처리하기. ReadAllText 메서드를 비동기로 처리하기 입니다. 별도의 스레드를 이용하거나 델리게이트의 BeginInvoke로 처리하여 비동기를 적용하는 예제입니다.(복잡함) using System; using System.IO; namespace TaskSample { class Program { public delegate string ReadAllTextDelegate(string path); static void Main(string[] args) { string filePath = @"C:\windows\system32\drivers\etc\HOSTS"; ReadAllTextDelegate func = File.ReadAllText; func.BeginInvoke(filePath, actionCompl.. 2019. 6. 9.
C# Task await 없이 Task타입을 단독으로 사용하는 예제 await 없이 Task타입을 단독으로 사용하는 예제 입니다. Task 타입은 반환값이 없는 경우 사용되며, Task 타입은 TResult 형식 매개 변수로 지정된 반환값이 있는 경우로 구분됩니다. using System; using System.Threading; using System.Threading.Tasks; namespace TaskSample { class Program { static void Main(string[] args) { ThreadPool.QueueUserWorkItem((obj) => { Console.WriteLine("process workItem"); }, null); Task task1 = new Task(() => { Console.WriteLine("Process .. 2019. 6. 9.
C# Task 비동기 API WInform 예제 using System; using System.IO; using System.Threading.Tasks; using System.Windows.Forms; namespace FileCopy { /// /// 메인폼 클래스 입니다. /// public partial class MainForm : Form { // Constructor (Public) #region MainForm() - 생성자 입니다. /// /// 생성자 입니다. /// public MainForm() { InitializeComponent(); #region 이벤트를 설정합니다. this.sourceButton.Click += sourceButton_Click; this.targetButton.Click += targetButton.. 2019. 6. 8.
300x250