2013년 9월 5일 목요일

c# 에서 Queue 사용

어떤 객체에 대해서, 한 곳에서 처리하기 위해 간단히 Queue 를 사용해 본다
Transaction 이 거시기 한 Cubrid( invalid buffer position 등과 같은 Exception Error ) 에, 짧은 순간 많은 쿼리를 처리 하거나 할 때...

선언
using System.Collections;
public Queue m_sqlQueue = new Queue();


구현
        public override bool SQLAdd(string sQuery)
        {
            try
            {
                m_sqlQueue.Enqueue(sQuery);
                return true;
            }
            catch (Exception ex)
            {
                MiddlewareLog.LogDebug(ex);
                return false;
            }

        }

// 아래 예제 스레드는 간단하지만, diffTick 을 20ms 으로 고정하지 않고, 짱구를 좀 굴리면 좀 더 최적화 해서 처리 할 수 있다, 여기선 Pass
        private void DoDBApply()
        {
            try
            {
                int startTick = Environment.TickCount;
                int currTick = 0;
                int diffTick = 0;
                do
                {
                    System.Threading.Thread.Sleep(1);

                    if (bShutdown == true) break;

                    currTick = Environment.TickCount;
                    diffTick = currTick - startTick;
                    if (diffTick > 20)
                    {
                        startTick = Environment.TickCount;
                        diffTick = 0;

                        if (m_sqlQueue.Count > 0)
                        {
                            object str = m_sqlQueue.Dequeue();
                            QueryExecute((string)str);
                        }
                    }
                } while (bShutdown == false);
            }
            catch
            {
            }
        }

댓글 없음: