Beispiel #1
0
        private void StartTradeThread()
        {
            // initialize data to use in thread
            tradeStartTime      = DateTime.Now;
            lastOtherActionTime = DateTime.Now;

            var pollThread = new Thread(() =>
            {
                IsTradeThreadRunning = true;

                DebugPrint("Trade thread starting.");

                // main thread loop for polling
                while (IsTradeThreadRunning)
                {
                    Thread.Sleep(TradePollingInterval);

                    try
                    {
                        bool action = trade.Poll();

                        if (action)
                        {
                            lastOtherActionTime = DateTime.Now;
                        }

                        if (trade.OtherUserCancelled || trade.HasTradeCompletedOk)
                        {
                            IsTradeThreadRunning = false;

                            trade.FireOnCloseEvent();
                        }

                        CheckTradeTimeout(trade);
                    }
                    catch (Exception ex)
                    {
                        // TODO: find a new way to do this w/o the trade events
                        //if (OnError != null)
                        //    OnError("Error Polling Trade: " + e);

                        // ok then we should stop polling...
                        IsTradeThreadRunning = false;
                        DebugPrint("[TRADEMANAGER] general error caught: " + ex);
                    }
                }

                DebugPrint("Trade thread shutting down.");

                if (OnTradeEnded != null)
                {
                    OnTradeEnded(this, null);
                }
            });

            pollThread.Start();
        }
Beispiel #2
0
        /// <summary>
        /// Starts the actual trade-polling thread.
        /// </summary>
        public void StartTradeThread(Trade trade)
        {
            // initialize data to use in thread
            tradeStartTime      = DateTime.Now;
            lastOtherActionTime = DateTime.Now;
            lastTimeoutMessage  = DateTime.Now.AddSeconds(-1000);

            var pollThread = new Thread(() =>
            {
                IsTradeThreadRunning = true;

                DebugPrint("Trade thread starting.");

                // main thread loop for polling
                try
                {
                    while (IsTradeThreadRunning)
                    {
                        bool action = trade.Poll();

                        if (action)
                        {
                            lastOtherActionTime = DateTime.Now;
                        }

                        if (trade.HasTradeEnded || CheckTradeTimeout(trade))
                        {
                            IsTradeThreadRunning = false;
                            break;
                        }

                        Thread.Sleep(TradePollingInterval);
                    }
                }
                catch (Exception ex)
                {
                    // TODO: find a new way to do this w/o the trade events
                    //if (OnError != null)
                    //    OnError("Error Polling Trade: " + e);

                    // ok then we should stop polling...
                    IsTradeThreadRunning = false;
                    DebugPrint("[TRADEMANAGER] general error caught: " + ex);
                    trade.FireOnErrorEvent("Unknown error occurred: " + ex.ToString());
                }
                finally
                {
                    DebugPrint("Trade thread shutting down.");
                    try     //Yikes, that's a lot of nested 'try's.  Is there some way to clean this up?
                    {
                        if (trade.IsTradeAwaitingConfirmation)
                        {
                            trade.FireOnAwaitingConfirmation();
                        }
                    }
                    catch (Exception ex)
                    {
                        trade.FireOnErrorEvent("Unknown error occurred during OnTradeAwaitingConfirmation: " + ex.ToString());
                    }
                    finally
                    {
                        try
                        {
                            //Make sure OnClose is always fired after OnSuccess, even if OnSuccess throws an exception
                            //(which it NEVER should, but...)
                            trade.FireOnCloseEvent();
                        }
                        catch (Exception e)
                        {
                            DebugError("Error occurred during trade.OnClose()! " + e);
                            throw;
                        }
                    }
                }
            });

            pollThread.Start();
        }
Beispiel #3
0
        /// <summary>
        /// Starts the actual trade-polling thread.
        /// </summary>
        public void StartTradeThread(Trade trade)
        {
            // initialize data to use in thread
            tradeStartTime = DateTime.Now;
            lastOtherActionTime = DateTime.Now;
            lastTimeoutMessage = DateTime.Now.AddSeconds(-1000);

            var pollThread = new Thread (() =>
                {
                    IsTradeThreadRunning = true;

                    DebugPrint ("Trade thread starting.");

                    // main thread loop for polling
                    try
                    {
                        while(IsTradeThreadRunning)
                        {
                            bool action = trade.Poll();

                            if(action)
                                lastOtherActionTime = DateTime.Now;

                            if (trade.HasTradeEnded || CheckTradeTimeout(trade))
                            {
                                IsTradeThreadRunning = false;
                                break;
                            }

                            Thread.Sleep(TradePollingInterval);
                        }
                    }
                    catch(Exception ex)
                    {
                        // TODO: find a new way to do this w/o the trade events
                        //if (OnError != null)
                        //    OnError("Error Polling Trade: " + e);

                        // ok then we should stop polling...
                        IsTradeThreadRunning = false;
                        DebugPrint("[TRADEMANAGER] general error caught: " + ex);
                        trade.FireOnErrorEvent("Unknown error occurred: " + ex.ToString());
                    }
                    finally
                    {
                        DebugPrint("Trade thread shutting down.");
                        try
                        {
                            try //Yikes, that's a lot of nested 'try's.  Is there some way to clean this up?
                            {
                                if(trade.HasTradeCompletedOk)
                                    trade.FireOnSuccessEvent();
                                else if(trade.IsTradeAwaitingConfirmation)
                                    trade.FireOnAwaitingConfirmation();
                            }
                            finally
                            {
                                //Make sure OnClose is always fired after OnSuccess, even if OnSuccess throws an exception
                                //(which it NEVER should, but...)
                                trade.FireOnCloseEvent();
                            }
                        }
                        catch(Exception ex)
                        {
                            trade.FireOnErrorEvent("Unknown error occurred DURING CLEANUP(!?): " + ex.ToString());
                        }
                    }
                });

            pollThread.Start();
        }
Beispiel #4
0
        private void StartTradeThread()
        {
            // initialize data to use in thread
            tradeStartTime      = DateTime.Now;
            lastOtherActionTime = DateTime.Now;

            var pollThread = new Thread(() =>
            {
                IsTradeThreadRunning = true;

                Console.WriteLine("Trade thread starting.");

                // main thread loop for polling
                while (IsTradeThreadRunning)
                {
                    Thread.Sleep(TradePollingInterval);

                    try
                    {
                        bool action = trade.Poll();

                        if (action)
                        {
                            lastOtherActionTime = DateTime.Now;
                        }

                        if (trade.OtherUserCancelled)
                        {
                            IsTradeThreadRunning = false;

                            try
                            {
                                trade.CancelTrade();
                            }
                            catch (Exception)
                            {
                                // ignore. possibly log. We don't care if the Cancel web command fails here we just want
                                // to fire the OnClose event.
                                DebugPrint("[TRADEMANAGER] error trying to cancel from poll thread");
                            }
                        }

                        //CheckTradeTimeout (trade);
                    }
                    catch (Exception ex)
                    {
                        // TODO: find a new way to do this w/o the trade events
//                        if (OnError != null)
//                            OnError ("Error Polling Trade: " + e);

                        // ok then we should stop polling...
                        //IsTradeThreadRunning = true;
                        Console.WriteLine("[TRADEMANAGER] general error caught: " + ex);
                    }
                }

                DebugPrint("Trade thread shutting down.");

                if (OnTradeEnded != null)
                {
                    OnTradeEnded(this, null);
                }
            });

            pollThread.Start();
        }