新文章

2012年8月23日 星期四

[C#]Thread4種範例簡單實作

分別使用1.EndInvoke , 2.WaitHandle , 3.LoopCheck , 4.Feedback
來實作簡單的Thread.



1.EndInvoke
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace AsynchronousSample
{
    //Sample1.Using EndInvoke
    class Sample1
    {
 
        public delegate void PrintDelegate(string content);
        static void Main()
        {
            //目前processID
            int threadID = Thread.CurrentThread.ManagedThreadId;
 
            //委託變數prinDelegate參考類class Sample1的Print
            PrintDelegate prinDelegate = Sample1.Print;
            Console.WriteLine("[The main process id:{0}]\tStart call print method...", threadID);
            //呼叫委託的BeginInvoke,開始非同步呼叫委託清單中的目標方法:Print
            IAsyncResult result = prinDelegate.BeginInvoke("Hello world!", null, null);
            //呼叫委託的EndInvoke,等待非同步呼叫結束
            prinDelegate.EndInvoke(result);
            Console.ReadLine();
        }
 
        public static void Print(string content)
        {
            int threadID = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("[The current process id:{0}]\t{1}", threadID, content);
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("[The current process id:{0}]\tThe prind method is end", threadID);
        }
 
    }
}
2.WaitHandle
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace AsynchronousSample
{
    //Sample2.Using WaitHandle
    class Sample1
    {
 
        public delegate void PrintDelegate(string content);
        static void Main()
        {
            //目前processID
            int threadID = Thread.CurrentThread.ManagedThreadId;
 
            //委託變數prinDelegate參考類class Sample1的Print
            PrintDelegate prinDelegate = Sample1.Print;
            Console.WriteLine("[The main process id:{0}]\tStart call print method...", threadID);
            //呼叫委託的BeginInvoke,開始非同步呼叫委託清單中的目標方法:Print
            IAsyncResult result = prinDelegate.BeginInvoke("Hello world!", null, null);
 
            //WaitOne(x,y):x為等待時間,y為等待前是否退出上下文的同步區域,並在稍後進行重新獲取
            result.AsyncWaitHandle.WaitOne(5000, false);
            Console.ReadLine();
        }
 
        public static void Print(string content)
        {
            int threadID = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("[The current process id:{0}]\t{1}", threadID, content);
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("[The current process id:{0}]\tThe prind method is end", threadID);
        }
 
    }
}

3.LoopCheck
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace AsynchronousSample
{
    //Sample3.Using LoopCheck
    class Sample1
    {
 
        public delegate void PrintDelegate(string content);
        static void Main()
        {
            //目前processID
            int threadID = Thread.CurrentThread.ManagedThreadId;
 
            //委託變數prinDelegate參考類class Sample1的Print
            PrintDelegate prinDelegate = Sample1.Print;
            Console.WriteLine("[The main process id:{0}]\tStart call print method...", threadID);
            //呼叫委託的BeginInvoke,開始非同步呼叫委託清單中的目標方法:Print
            IAsyncResult result = prinDelegate.BeginInvoke("Hello world!", null, null);
            //檢查是否完成
            while (!result.IsCompleted)
            {
                Console.WriteLine(" . ");
                Thread.Sleep(500);
            }
        }
 
        public static void Print(string content)
        {
            int threadID = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("[The current process id:{0}]\t{1}", threadID, content);
            System.Threading.Thread.Sleep(2000);
            Console.WriteLine("[The current process id:{0}]\tThe prind method is end", threadID);
        }
 
    }
}
4.Feedback
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace AsynchronousSample
{
    //Sample4.Using FeedBack
    class Sample1
    {
 
        public delegate void PrintDelegate(string content);
        static void Main()
        {
            //目前processID
            int threadID = Thread.CurrentThread.ManagedThreadId;
 
            //委託變數prinDelegate參考類class Sample1的Print
            PrintDelegate prinDelegate = Sample1.Print;
            Console.WriteLine("[The main process id:{0}]\tStart call print method...", threadID);
            //呼叫委託的BeginInvoke,開始非同步呼叫委託清單中的目標方法:Print
            IAsyncResult result = prinDelegate.BeginInvoke("Hello world!", PrintComplete, prinDelegate);
            Thread.Sleep(10000);
           
        }
 
        public static void Print(string content)
        {
            int threadID = Thread.CurrentThread.ManagedThreadId;
            Console.WriteLine("[The current process id:{0}]\t{1}", threadID, content);
            System.Threading.Thread.Sleep(3000);
            
        }
 
        private static void PrintComplete(IAsyncResult asyncResult)
        {
            if (null == asyncResult)
            {
                throw new ArgumentNullException();
            }
 
            int threadID = Thread.CurrentThread.ManagedThreadId;
            (asyncResult.AsyncState as PrintDelegate).EndInvoke(asyncResult);
            Console.WriteLine("[The current process id:{0}]\tThe prind method is end", threadID);
        }
    }
}

沒有留言:

張貼留言