using System.Threading;

public partial class Form1 : Form
    {
        delegate int WelkinDelegate(string Data,int Num);

        WelkinDelegate yesyes = new WelkinDelegate(Hello);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //無參數,無回傳值
            ThreadStart welkin1 = new ThreadStart(mymy);
            Thread Cool = new Thread(welkin1);
            Cool.Start();

            //有參數,無回傳值
            ParameterizedThreadStart welkin2 = new ParameterizedThreadStart(P_mymy);
            Thread Cool2 = new Thread(welkin2);
            Cool2.Start("GoGo");

            //應用程式集區(ThreadPool)
            WaitCallback WC = new WaitCallback(P_mymy);
            if (ThreadPool.QueueUserWorkItem(WC))
            {
                //完成
            }


            //執行緒環境(Execution Context)
            //暫停傳遞執行緒環境
            ExecutionContext.SuppressFlow();
            //恢復
            ExecutionContext.RestoreFlow();
            //複製執行緒環境
            ExecutionContext.Capture();
            //用Run函式可用在指定的環境中

            //System.Threaing裡面的Timer
            System.Threading.Timer  myTimer= new System.Threading.Timer(new TimerCallback(P_mymy), null, 0, 3000);
            

            //共用資源用法
            //==================================================
            //鎖定一次只能有一個執行緒執行
            lock (this)
            {
                
            }

            //Monitor類別
            Monitor.Enter(this);
            //接下來的部分開始鎖定
            Monitor.Exit(this);

            //Mutex類別,用在鎖定Process或是AppDomain

            Mutex Mu = new Mutex();
            //接下來的部分開始鎖定
            Mu.WaitOne(3000, false);
          
            Mu.ReleaseMutex();

            //Semaphore類別,控制執行緒數量
            Semaphore SP = new Semaphore(1,5);
            //將執行緒加入到這個Semaphore物件裡面
            //加入一個執行緒,Semaphore的計數器就會減1
            //到0後就不能再加入了
            SP.WaitOne();
            //釋放計數器的數量的一個
            SP.Release();

            //ReaderWriterLock類別
            ReaderWriterLock RW = new ReaderWriterLock();
            //四種函式
            //AcquireReaderLock
            //ReleaseReaderLock;
            //AcquireWriterLock;
            //ReleaseWriterLock;


            //非同步程式開發模型(Asynchronous Programming Model)
            //1,Begin方法會回傳IAsyncResult型別的物件,可判斷IsComplete
            //2.呼叫了Begin方法後,可直接呼叫End方法,就會等待執行完傳回東西
            //3.用AsyncCallback的方法,如下例
            //先在最上面宣告一個Delegate和用一個WelkinDelegate的物件
            AsyncCallback AC = new AsyncCallback(CallbackMM);
            yesyes.BeginInvoke("WW", 234, AC, null);

        }
        private void mymy()
        {
            //Something
        }
        private void P_mymy(object Data)
        {

        }
        private static int Hello(string Data,int Num)
        {
            

            return 0;
        }
        private void CallbackMM(IAsyncResult IAR)
        {
            int Result = yesyes.EndInvoke(IAR);
            
        }
    }

welkingunther 發表在 痞客邦 PIXNET 留言(0) 人氣()