'programming'에 해당되는 글 387건

  1. 2011.04.20 Thread Class

Thread Class

programming/C# 2011. 4. 20. 00:13
반응형
http://msdn.microsoft.com/en-us/library/bkb1k2x8(vs.71).aspx

Example

[Visual Basic, C#, C++] The following code example demonstrates simple threading functionality.

[Visual Basic] 
Imports System
Imports System.Threading

' Simple threading scenario:  Start a Shared method running
' on a second thread.
Public Class ThreadExample
    ' The ThreadProc method is called when the thread starts.
    ' It loops ten times, writing to the console and yielding 
    ' the rest of its time slice each time, and then ends.
    Public Shared Sub ThreadProc()
        Dim i As Integer
        For i = 0 To 9
            Console.WriteLine("ThreadProc: {0}", i)
            ' Yield the rest of the time slice.
            Thread.Sleep(0)
        Next
    End Sub

    Public Shared Sub Main()
        Console.WriteLine("Main thread: Start a second thread.")
        ' The constructor for the Thread class requires a ThreadStart 
        ' delegate.  The Visual Basic AddressOf operator creates this
        ' delegate for you.
        Dim t As New Thread(AddressOf ThreadProc)
        ' Start ThreadProc.  On a uniprocessor, the thread does not get 
        ' any processor time until the main thread yields.  Uncomment 
        ' the Thread.Sleep that follows t.Start() to see the difference.
        t.Start()
        'Thread.Sleep(0)

        Dim i As Integer
        For i = 1 To 4
            Console.WriteLine("Main thread: Do some work.")
            Thread.Sleep(0)
        Next

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.")
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.")
        Console.ReadLine()
    End Sub
End Class

[C#] 
using System;
using System.Threading;

// Simple threading scenario:  Start a static method running
// on a second thread.
public class ThreadExample {
    // The ThreadProc method is called when the thread starts.
    // It loops ten times, writing to the console and yielding 
    // the rest of its time slice each time, and then ends.
    public static void ThreadProc() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("ThreadProc: {0}", i);
            // Yield the rest of the time slice.
            Thread.Sleep(0);
        }
    }

    public static void Main() {
        Console.WriteLine("Main thread: Start a second thread.");
        // The constructor for the Thread class requires a ThreadStart 
        // delegate that represents the method to be executed on the 
        // thread.  C# simplifies the creation of this delegate.
        Thread t = new Thread(new ThreadStart(ThreadProc));
        // Start ThreadProc.  On a uniprocessor, the thread does not get 
        // any processor time until the main thread yields.  Uncomment 
        // the Thread.Sleep that follows t.Start() to see the difference.
        t.Start();
        //Thread.Sleep(0);

        for (int i = 0; i < 4; i++) {
            Console.WriteLine("Main thread: Do some work.");
            Thread.Sleep(0);
        }

        Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
        t.Join();
        Console.WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
        Console.ReadLine();
    }
}

[C++] 

// [C++]
// Compile using /clr option.
#using <mscorlib.dll>
 using namespace System;
 using namespace System::Threading;
 // Simple threading scenario:  Start a Shared method running
 // on a second thread.
 public __gc class ThreadExample 
 {
 public:
     // The ThreadProc method is called when the thread starts.
     // It loops ten times, writing to the console and yielding 
     // the rest of its time slice each time, and then ends.
     static void ThreadProc()
     {
         for (int i = 0; i < 10; i++) 
         {
             Console::Write("ThreadProc: ");
             Console::WriteLine(i);
             // Yield the rest of the time slice.
             Thread::Sleep(0);
         }
     }
 };
 
 int main() 
 {
     Console::WriteLine(S"Main thread: Start a second thread.");
     // Create the thread, passing a ThreadStart delegate that
     // represents the ThreadExample::ThreadProc method.  For a 
     // delegate representing a static method, no object is
     // required.
     Thread *oThread = new Thread(new ThreadStart(0, &ThreadExample::ThreadProc));
 
     // Start the thread.  On a uniprocessor, the thread does not get 
     // any processor time until the main thread yields.  Uncomment
     // the Thread.Sleep that follows t.Start() to see the difference.
     oThread->Start();
     //Thread::Sleep(0);

     for (int i = 0; i < 4; i++) {
         Console::WriteLine("Main thread: Do some work.");
         Thread::Sleep(0);
     }

     Console::WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
     oThread->Join();
     Console::WriteLine("Main thread: ThreadProc.Join has returned.  Press Enter to end program.");
     Console::ReadLine();
     return 0;
 }

[Visual Basic, C#, C++] This code produces output similar to the following:

[VB, C++, C#]
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned.  Press Enter to end program.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

반응형
Posted by 공간사랑
,