๐Task
Using Task
static void Main()
{
Action<object> action = (object obj) =>
{
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId,
obj,
Thread.CurrentThread.ManagedThreadId);
};
Action<object> action1 = (object obj) =>
{
int[] abc = obj as int[];
Console.WriteLine("{0}, {1}, {2}", abc[0], abc[1], abc[2]);
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId,
abc,
Thread.CurrentThread.ManagedThreadId);
};
// Create a task but do not start it.
Task t1 = new Task(action, "alpha");
// Construct a started task
int[] asdf = { 1, 2, 3 };
//Task t2 = Task.Factory.StartNew(action, asdf);
//Task t2 = Task.Factory.StartNew(action, new int[3] { 1, 2, 3 });
Task t2 = Task.Factory.StartNew(action1, new int[] { 1, 2, 3 });
//Task t2 = Task.Factory.StartNew(action, "beta");
// Block the main thread to demonstrate that t2 is executing
t2.Wait(3000);
Task aa = new Task((object a) =>
{
int[] abc = a as int[];
Console.WriteLine("{0}, {1}", abc[0], abc[1]);
}, new int[] {1,2});
aa.Start();
// Launch t1
t1.Wait(2000);
t1.Start();
Console.WriteLine("t1 has been launched. (Main Thread={0})",
Thread.CurrentThread.ManagedThreadId);
// Wait for the task to finish.
t1.Wait();
// Construct a started task using Task.Run.
String taskData = "delta";
Task t3 = Task.Run(() =>
{
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, taskData,
Thread.CurrentThread.ManagedThreadId);
});
// Wait for the task to finish.
t3.Wait();
// Construct an unstarted task
Task t4 = new Task(action, "gamma");
// Run it synchronously
t4.RunSynchronously();
// Although the task was run synchronously, it is a good practice
// to wait for it in the event exceptions were thrown by the task.
t4.Wait();
}
Task.Start()
๋ฑ๋ก๋ task start
Task.Wait()
ํ๋ผ๋ฏธํฐ๋ millisec value int ์ด๋ฉฐ
start() ํ์ง ์๊ณ ๋งค๊ฐ๋ณ์์๋ wait() ์ด๋ผ๋ฉด ๋ฌดํ๋๊ธฐ
start() ํ ๋ค, wait()๋ task๊ฐ ๋๋ ๋๊น์ง ๋๊ธฐ
wait(1000) ๊ฐ์ด ์๋ค๋ฉด, ํด๋น ์๊ฐ๋งํผ ๋
Task.Factory.StartNew(Action, object)
์ฆ ์ ์ค ํ
Task.Start() ํ Task.Wait() ์ํด์ฃผ๋ฉด?
static void Main()
{
Action<object> action = (object obj) =>
{
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId,
obj,
Thread.CurrentThread.ManagedThreadId);
};
Action<object> action1 = (object obj) =>
{
int[] abc = obj as int[];
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId,
abc,
Thread.CurrentThread.ManagedThreadId);
};
// Create a task but do not start it.
Task t1 = new Task(action, "alpha");
// Construct a started task
int[] asdf = { 1, 2, 3 };
//Task t2 = Task.Factory.StartNew(action, asdf);
//Task t2 = Task.Factory.StartNew(action, new int[3] { 1, 2, 3 });
Task t2 = Task.Factory.StartNew(action1, new int[] { 1, 2, 3 });
//Task t2 = Task.Factory.StartNew(action, "beta");
// Block the main thread to demonstrate that t2 is executing
t2.Wait(3000);
Task aa = new Task((object a) =>
{
int[] abc = a as int[];
Console.WriteLine("{0}, {1}", abc[0], abc[1]);
}, new int[] {1,2});
aa.Start();
// Launch t1
t1.Wait(2000);
t1.Start();
Console.WriteLine("t1 has been launched. (Main Thread={0})",
Thread.CurrentThread.ManagedThreadId);
// Wait for the task to finish.
//t1.Wait();
// Construct a started task using Task.Run.
String taskData = "delta";
Task t3 = Task.Run(() =>
{
Console.WriteLine("Task={0}, obj={1}, Thread={2}",
Task.CurrentId, taskData,
Thread.CurrentThread.ManagedThreadId);
});
// Wait for the task to finish.
//t3.Wait();
// Construct an unstarted task
Task t4 = new Task(action, "gamma");
// Run it synchronously
t4.RunSynchronously();
// Although the task was run synchronously, it is a good practice
// to wait for it in the event exceptions were thrown by the task.
t4.Wait();
}
์ํ๋ ์ํ์ค๋๋ก ๋์ค์ง ์์ ์ ์๋ค.
Last updated