private void OrderThreadProc(CancellationToken ct)
        {
            List <RequestContent> cancelQuoteOrderLst = new List <RequestContent>();
            List <RequestContent> quoteOrderLst       = new List <RequestContent>();
            List <RequestContent> cancelOrderLst      = new List <RequestContent>();
            List <RequestContent> orderLst            = new List <RequestContent>();

            while (!ct.IsCancellationRequested)//(true)
            {
                try
                {
                    //CtpRequestContent cmd = _OrdQueue.Dequeue();
                    //Util.Log("TradeApi OrderThreadProc: " + cmd.FunctionName + " dequeues.");
                    //MethodInfo serverMethod = CtpDataServer.getServerInstance().GetType().GetMethod(cmd.FunctionName);
                    //serverMethod.Invoke(CtpDataServer.getServerInstance(), cmd.Params.ToArray());

                    List <RequestContent> dataList = _OrdQueue.DequeueAll();
                    foreach (RequestContent cmd in dataList)
                    {
                        if (cmd.FunctionName == "CancelQuoteOrder")
                        {
                            cancelQuoteOrderLst.Add(cmd);
                        }
                        if (cmd.FunctionName == "NewQuoteOrder")
                        {
                            quoteOrderLst.Add(cmd);
                        }
                        if (cmd.FunctionName == "CancelOrder")
                        {
                            cancelOrderLst.Add(cmd);
                        }
                        if (cmd.FunctionName == "NewOrderSingle")
                        {
                            orderLst.Add(cmd);
                        }

                        Util.Log("TradeApi OrderThreadProc: " + cmd.FunctionName + " dequeues.");
                        MethodInfo serverMethod = DataContainer.GetUserInstance().GetType().GetMethod(cmd.FunctionName);
                        serverMethod.Invoke(DataContainer.GetUserInstance(), cmd.Params.ToArray());
                        //Thread.Sleep(175);
                    }

                    dataList.Clear();
                    dataList.AddRange(cancelQuoteOrderLst);
                    dataList.AddRange(quoteOrderLst);
                    dataList.AddRange(cancelOrderLst);
                    dataList.AddRange(orderLst);
                    if (dataList.Count > 0)
                    {
                        Util.Log(string.Format("Order Count: {0}; Cancel Quote Order: {1}, Quote Order: {2}, Cancel Order: {3}, Order: {4}"
                                               , dataList.Count, cancelQuoteOrderLst.Count, quoteOrderLst.Count, cancelOrderLst.Count, orderLst.Count));
                    }
                    cancelQuoteOrderLst.Clear();
                    quoteOrderLst.Clear();
                    cancelOrderLst.Clear();
                    orderLst.Clear();
                }
                catch (Exception ex)
                {
                    Util.Log_Error("exception: " + ex.Message);
                    Util.Log_Error("exception: " + ex.StackTrace);
                    cancelQuoteOrderLst.Clear();
                    quoteOrderLst.Clear();
                    cancelOrderLst.Clear();
                    orderLst.Clear();
                    ct.ThrowIfCancellationRequested();
                }
            }
            if (_OrdTask.IsCompleted)
            {
                _OrdTask.Dispose();
            }
            else
            {
                Util.Log("OrdTask Status: " + _OrdTask.Status);
            }
        }
        private void ResponseThreadProc(CancellationToken ct)
        {
            List <CThostFtdcOrderField>           orderDataList      = new List <CThostFtdcOrderField>();
            List <CThostFtdcTradeField>           tradeDataList      = new List <CThostFtdcTradeField>();
            List <CThostFtdcQuoteField>           quoteOrderDataList = new List <CThostFtdcQuoteField>();
            List <CThostFtdcExecOrderField>       execOrderDataList  = new List <CThostFtdcExecOrderField>();
            List <CThostFtdcDepthMarketDataField> marketDataList     = new List <CThostFtdcDepthMarketDataField>();

            while ((!ct.IsCancellationRequested))//(ExecFlag)
            {
                try
                {
                    // Dequeue All
                    //int count = _ResponseQueue.Count;
                    //List<object> dataList = new List<object>();
                    //for (int i = 0; i < count; i++)
                    //{
                    //    object item;
                    //    if (_ResponseQueue.TryDequeue(out item))
                    //    {
                    //        dataList.Add(item);
                    //    }
                    //}
                    List <object> dataList = _ResponseQueue.DequeueAll();
                    foreach (object data in dataList)
                    {
                        if (_BackEnd == BACKENDTYPE.CTP)
                        {
                            if (data is CThostFtdcOrderField)
                            {
                                orderDataList.Add((CThostFtdcOrderField)data);
                            }
                            else if (data is CThostFtdcTradeField)
                            {
                                tradeDataList.Add((CThostFtdcTradeField)data);
                            }
                            else if (data is CThostFtdcQuoteField)
                            {
                                quoteOrderDataList.Add((CThostFtdcQuoteField)data);
                            }
                            else if (data is CThostFtdcExecOrderField)
                            {
                                execOrderDataList.Add((CThostFtdcExecOrderField)data);
                            }
                            else if (data is CThostFtdcDepthMarketDataField)
                            {
                                marketDataList.Add((CThostFtdcDepthMarketDataField)data);
                            }
                            else
                            {
                                Util.Log("RspQueue: Error for CTP Response Data!");
                            }
                        }
                        else if (_BackEnd == BACKENDTYPE.FEMAS)
                        {
                            //if (data is CUstpFtdcOrderField)
                            //{
                            //    orderDataList.Add((CUstpFtdcOrderField)data);
                            //}
                            //else if (data is CThostFtdcTradeField)
                            //{
                            //    tradeDataList.Add((CUstpFtdcTradeField)data);
                            //}
                            //else if (data is CUstpFtdcDepthMarketDataField)
                            //{
                            //    marketDataList.Add((CUstpFtdcDepthMarketDataField)data);
                            //}
                            //else
                            //{
                            //    Util.Log("RspQueue: Error for CTP Response Data!");
                            //}
                        }
                    }

                    if (orderDataList.Count > 0)
                    {
                        Util.Log("Processing Order Data...");
                        CreateFromOrderData(orderDataList);
                        orderDataList.Clear();
                    }
                    if (tradeDataList.Count > 0)
                    {
                        Util.Log("Processing Trade Data...");
                        CreateFromTradeData(tradeDataList);
                        tradeDataList.Clear();
                    }
                    if (quoteOrderDataList.Count > 0)
                    {
                        Util.Log("Processing Quote Order Data...");
                        CreateFromQuoteOrderData(quoteOrderDataList);
                        quoteOrderDataList.Clear();
                    }
                    if (execOrderDataList.Count > 0)
                    {
                        Util.Log("Processing Execution Data...");
                        CreateFromExecOrderData(execOrderDataList);
                        execOrderDataList.Clear();
                    }
                    if (marketDataList.Count > 0)
                    {
                        CreateFromMarketData(marketDataList);
                        marketDataList.Clear();
                    }
                    //if (dataList.Count > 0)
                    //{
                    //    Util.Log("Response Thread has processed all data!");
                    //}
                }
                catch (Exception ex)
                {
                    Util.Log("exception: " + ex.Message);
                    Util.Log("exception: " + ex.StackTrace);
                    orderDataList.Clear();
                    tradeDataList.Clear();
                    quoteOrderDataList.Clear();
                    execOrderDataList.Clear();
                    marketDataList.Clear();
                    ct.ThrowIfCancellationRequested();
                }
            }
        }