Объект принадлежит другому потоку


Объект принадлежит другому потоку (Locked)
Atom
11/24/2011


При попытке построить свечки в методе DrawCandles возникает ошибка, что объект candles принадлежит другому потому, как с этим бороться?


Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Controls;
using StockSharp.Algo;
using StockSharp.Algo.Logging;
using StockSharp.AlfaDirect;
using StockSharp.BusinessEntities;
using ADLite;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using Ecng.Collections;
using Ecng.Common;
using System.Collections.ObjectModel;
using MessageBox = System.Windows.MessageBox;
using StockSharp.Xaml;
using System.Xml;
using Syncfusion.Windows.Shared;
using Ecng.ComponentModel;
using StockSharp.Algo.Candles;
using AmCharts.Windows.Stock;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;




namespace MyStock
{
    public partial class MainWindow
    {
        private readonly Dictionary<CandleToken, MainWindow> chart = new Dictionary<CandleToken, MainWindow>();
        public AlfaTrader Trader;
        public bool _isConnected;
        public Portfolio _portfolio;
        public CandleManager _candleManager;
        private readonly ObservableCollection<Security> _securitiesSource = new ObservableCollection<Security>();
        private readonly LogManager _logManager = new LogManager();
        public readonly ICollection<CustomChartIndicator> _ind1;
        private DateTime _lastCandleTime;
        public readonly TimeSpan timeFrame = (TimeSpan)(AlfaTimeFrames.Minute1);

        public Ind _ind { get; private set; }

        public MainWindow()
            
        {
            InitializeComponent();

            Security.ItemsSource = _securitiesSource;

            Security_2.ItemsSource = _securitiesSource;

            var From = DateTime.Today - TimeSpan.FromDays(1);
            var To = DateTime.Now;

            _logManager.Listeners.Add(new FileLogListener());
            
            _ind1 = Chart.CreateTrend("IND", GraphType.Line);

            var security = (Security)SelectedSecurity;

        }

        //private void DrawInd()
        //{
        //    var timeFrame = (TimeSpan)(AlfaTimeFrames.Minute1);

        //    var security = SelectedSecurity;

        //    var security_2 = SelectedSecurity_2;

        //    var bounds = timeFrame.GetCandleBounds(Trader);

        //    if ((_lastCandleTime + timeFrame) < bounds.Min)
        //    {
        //        var endOffset = TimeSpan.FromSeconds(1);

        //        bounds = new Range<DateTime>(_lastCandleTime + timeFrame, bounds.Min - endOffset);

        //        var candles = _candleManager.GetTimeFrameCandles(security, timeFrame, bounds);
        //        var canldes1 = _candleManager.GetTimeFrameCandles(security_2, timeFrame, bounds);

        //        if (candles.Count() > 0)
        //        {
        //            // получаем время самой последней свечки и запоминаем его как новое начало
        //            _lastCandleTime = candles.Max(c => c.Time);

        //            DrawIndLine(bounds.Min);
        //        }
        //    }
        //} 


        private void DrawIndLine(DateTime time)
        {

            _lastCandleTime = time;

                _ind1.Add(new CustomChartIndicator
                {
                    Time = time,
                    Value = (double)_ind.Value
                });
        }

        public void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!_isConnected)
                {

                    if (Trader == null)
                    {
                        var monitor = new LogWindow();
                        monitor.Show();

                        Trader = new AlfaTrader
                        {
                            Login = textBox1.Text,
                            Password = passwordBox1.Password
                        };

                        Trader.NewSecurities += securities => _securitiesSource.AddRange(securities);

                        Trader.NewPortfolios += portfolios => portfolios.ForEach(Trader.RegisterPortfolio);

                        Trader.NewSecurities += securities => securities.ForEach(Trader.RegisterTrades);

                        _logManager.Listeners.Add(new GuiLogListener(monitor));

                        _logManager.Sources.Add(Trader);

                        _candleManager = new CandleManager(Trader);

                        _candleManager.NewCandles += DrawCandles;
                        _candleManager.CandlesChanged += DrawCandles;

                    }

                    Trader.Connect();
                    Trader.StartExport();

                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(this, ex.Message, "Error");
                return;
            }
        }


        public void DrawCandles(CandleToken token, IEnumerable<Candle> candles)
        {
                var wnd = chart.TryGetValue(token);
                if (wnd != null)
                    wnd.Chart.Candles.AddRange(candles);
        }

        //public void DrawCandles1(IEnumerable<Candle> candles1)
        //{
        //    _candles.AddRange(candles1);
        //    Chart1._stockChart.DataSets[0].ItemsSource = _candles;
        //}

        protected override void OnClosing(CancelEventArgs e)
        {

            base.OnClosing(e);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
         Trader.Disconnect();
        }


        private void textBox1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {

        }

        private void passwordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {

        }


        public Security SelectedSecurity
        {
            get { return (Security)Security.SelectedValue; }
        }

        public Security SelectedSecurity_2
        {
            get { return (Security)Security_2.SelectedValue; }
        }


        public void SecuritySelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowChart.IsEnabled = SelectedSecurity != null;
        }

        public void Security_2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ShowChart.IsEnabled = SelectedSecurity_2 != null;
        }


        public void ShowChart_Click(object sender, RoutedEventArgs e)
        {
            _ind = new Ind();

            var security = (Security)SelectedSecurity;
            var security_2 = (Security)SelectedSecurity_2;
            var from = DateTime.Today - TimeSpan.FromDays(4);
            var to = DateTime.Now;

            var timeFrame = (TimeSpan)(AlfaTimeFrames.Minute1);

            CandleToken token;

            token = _candleManager.RegisterTimeFrameCandles(security, timeFrame);

            chart.SafeAdd(token);

            //foreach (var candle in candles)
            //{

            //    _lastCandleTime = candle.Time;

            //    DrawIndLine(candle.Time);
            //}

            //Chart.Candles.AddRange(candles);
            }


        private void Chart_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Chart1_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }


}







Tags:


Thanks:


Sergey Masyura

Avatar
Date: 11/25/2011


OvcharenkoVI Go to
При попытке построить свечки в методе DrawCandles возникает ошибка, что объект candles принадлежит другому потому, как с этим бороться?


Code
using System;

        //public void DrawCandles1(IEnumerable<Candle> candles1)
        //{
        //    _candles.AddRange(candles1);
        //    Chart1._stockChart.DataSets[0].ItemsSource = _candles;
        //}
    }


}








Code

this.GuiAsync(() => DrawCandles1(candles));
Thanks:

OvcharenkoVI

Avatar
Date: 11/25/2011


Да, я понял уже в чем ошибка) спасибо!
Thanks:


Attach files by dragging & dropping, , or pasting from the clipboard.

loading
clippy