๐Ÿ“šThread

using System.Threading;

class Program
{
    static Thread thread1, thread2;

    public static void Main()
    {
        thread1 = new Thread(ThreadProc);
        thread1.Name = "Thread1";
        thread1.Start();

        thread2 = new Thread(ThreadProc);
        thread2.Name = "Thread2";
        thread2.Start();
    }

    private static void ThreadProc()
    {
        Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
        if (Thread.CurrentThread.Name == "Thread1" &&
            thread2.ThreadState != ThreadState.Unstarted)
        {
            Console.WriteLine("๋‚˜๋Š” {0} thread2๊ฐ€ ๋๋‚ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.", Thread.CurrentThread.Name);
            thread2.Join(); // ํ˜„์žฌ ์Šค๋ ˆ๋“œ thread1์ด ์ง„ํ–‰์ค‘์— ๋‚จ์˜ ์Šค๋ ˆ๋“œ(thread2)๋ฅผ join()์‹œ์ผœ์„œ waitํ•˜๋Š”์ค‘.
            Console.WriteLine("๋‚˜๋Š” {0} thread2๊ฐ€ ๋๋‚˜์„œ ์ด์–ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค..", Thread.CurrentThread.Name);
        }

        Console.WriteLine("๋‚˜๋Š” {0} Sleep(4000) ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.",Thread.CurrentThread.Name);
        Thread.Sleep(4000);
        Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
        Console.WriteLine("Thread1: {0}", thread1.ThreadState);
        Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
    }
}

with Parameter

class Param1
{
    public string aa { get; set; }
    public Action<string, int> callback { get; set; }
    public int annon { get; set; }
    public HttpWebRequest request { get; set; }
}

{
    Param1 param1 = new Param1
    {
        aa = aa,
        callback = callback,
        annon = annon,
        request = request
    };
    
    Thread t1 = new Thread(new ParameterizedThreadStart(Func1));
    t1.Start(param1);
}

public static void Func1(object param)
{
    // ...
}       
                

Last updated