[C# thread ] 쓰레드...

쓰레드가 이해가 안되서 예제를 더이상 따라할 수 가 없다니.....
나.. 바본가


쓰레드를 사용하기위해선 .Net Framework 에서 어떤 class로 제공하는지를 알아야하지 않을까?
System.Threading.Thread 다. Namespace 를 사용해서 추가해주면 된다.
using System.Threading.Thread; 이딴 식으로 ~

static void DoSomething()
{
                 for (i=0; i< 5; i++)
            {
                        Console.WriteLine("DoSometing : {0}", i );
            }
}
이란 매소드가 있다고 가정하고

1. Thread 의 인스턴스를 생성한다. 이때 생성자의 매개변수로 스레드가 실행살 메소드를 매개변수로 넘긴다. < -- 한국말이 더 이상해

Thread t1 = new Thread(new ThreadStart (DoSomething));


2. Thread.Start() 메소드를 호출하여 스레드를 시작한다.

t1.Start();

3. Thread.Join() 메소드를 호출하여 스레드가 끝날대까지 기다린다.

t1.Join();

즉~

static void Main (string[] args)
{

Thread t1 = new Thread(new ThreadStart (DoSomething));
t1.Start(); 
t1.Join(); 


}



class MainApp
    {
        static void DoSomething()
        {
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("DoSomething : {0}", i);
                Thread.Sleep(10);
            }
        }


        static void Main(string[] args)
        {
            Thread t1 = new Thread(DoSomething);
            Console.WriteLine("Starting thread..");
            t1.Start();

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Main : {0}", i);
                Thread.Sleep(10);
            }
            Console.WriteLine("Watching until thread stops...");
            t1.Join();

            Console.WriteLine("Finished");


        }
    }



댓글

이 블로그의 인기 게시물

[C# Winform 예제] ScreenSaver 스크린세이버 만들기

아 정말 이사람 IELTS 관한 비법 글은.. 정말 공감10000%

Vocabulary for IELTS (2000 words) advanced level.