Exemplo n.º 1
0
        private void WokerJob(object status)
        {
            if (status is ManualResetEvent)
            {
                try
                {
                    ManualResetEvent myFlag = (ManualResetEvent)status;

                    while (true)
                    {
                        myFlag.WaitOne();

                        if (isStop)
                        {
                            break;
                        }

                        TData data = default(TData);
                        lock (queueData)
                        {
                            if (queueData.Count > 0)
                            {
                                data = queueData.Dequeue();
                            }
                        }
                        if (data != null && data.Equals(default(TData)) == false)
                        {
                            if (OnDataIn != null)
                            {
                                OnDataIn(data);
                            }
                        }
                        else
                        {
                            // 沒資料要處理時,就歸還Flag
                            myFlag.Reset();
                            lock (workerFlag)
                                workerFlag.Enqueue(myFlag);
                        }
                    }
                }
                catch (Exception exp)
                {
                    // ERROR
                    Console.WriteLine(exp.Message);
                }
            }
            else
            {
                throw new Exception("資料型別不是AutoResetEvent");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 執行緒要執行的函式
        /// </summary>
        /// <param name="status">同步用的物件</param>
        private void WokerJob(object status)
        {
            if (status is ManualResetEvent)
            {
                try
                {
                    ManualResetEvent myFlag = (ManualResetEvent)status;

                    #region worker core function
                    // Get data and notify with events
                    // If there is no more data , then return the flag
                    while (true)
                    {
                        myFlag.WaitOne();

                        TData data = default(TData);
                        lock (queueData)
                        {
                            if (queueData.Count > 0)
                            {
                                data = queueData.Dequeue();
                            }
                        }
                        if (data != null && data.Equals(default(TData)) == false)
                        {
                            if (OnDataIn != null)
                            {
                                OnDataIn(data);
                            }
                        }
                        else
                        {
                            // 沒資料要處理時,就歸還Flag
                            myFlag.Reset();
                            lock (workerFlag)
                                workerFlag.Enqueue(myFlag);
                        }
                    }
                    #endregion
                }
                catch (Exception exp)
                {
                    // ERROR
                    Console.WriteLine(exp.Message);
                }
            }
            else
            {
                throw new Exception("資料型別不是AutoResetEvent");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// 確認是否有資料,有的話就傳回,沒的話就傳回null
 /// </summary>
 /// <returns></returns>
 public TData GetIfAny()
 {
     lock (lock_out)
     {
         if (outContainer.Count > 0)
         {
             return(outContainer.Dequeue());
         }
         else
         {
             return(default(TData));
         }
     }
 }