๐Ÿ“š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