S
S
Shirixae2011-04-01 11:53:32
C++ / C#
Shirixae, 2011-04-01 11:53:32

.NET - Collection Sharing?

The problem is this: there is a subscription to an event that updates the collection.

this.Trader.NewOrders += orders => this.GuiAsync(() => _ordersWindow.Orders.AddRange(orders)); //GuiAsync это аналог Dispatcher.BeginInvoke из фреймворка, с которым работаю

there is a timer in which the function is called
public void OnTimedEvent(object source, ElapsedEventArgs e)
    {  
        if (MainWindow.Instance._tradesWindow.Trades[MainWindow.Instance._tradesWindow.Trades.Count() - 1].Time >= startTime)
        {
            var trade = MainWindow.Instance._tradesWindow.Trades[MainWindow.Instance._tradesWindow.Trades.Count() - 1];
            if (trade.Price != Cc)
                MainWindow.Instance.Dispatcher.BeginInvoke(new CheckNewStrikeA1Delegate(CheckNewStrikeA1), trade.Price);
        }  
    }

inside the function, some actions take place, after which _ordersWindow.Orders changes
void CheckNewStrikeA1(double strike)
{            
    lock (myLock2)
    {                    
        A1_P_51(strike);
    }    
 
    if (IsNewTrade() == false) //эта функция смотрит в _ordersWindow.Orders  
        return;

But, if _ordersWindow.Orders has changed after passing A1_P_51(strike), IsNewTrade does not see the changes. Sees only at the next tick of the timer.
Which way to look?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bacz, 2011-04-01
@bacz

Make common locks for adding, counting and checking, for example like this:

this.Trader.NewOrders += orders => this.GuiAsync(() => { lock(myLock2) { _ordersWindow.Orders.AddRange(orders); } }); //GuiAsync

void CheckNewStrikeA1(double strike)
{
  lock (myLock2)
  {
    A1_P_51(strike);
    if (IsNewTrade() == false) //эта функция смотрит в _ordersWindow.Orders
       return;
  }
}

If I understand you correctly.

S
Shirixae, 2011-04-01
@Shirixae

not quite, just the opposite =) during the execution of the A1_P_51 function , the this.Trader.NewOrders event occurs and at the time of calling IsNewTrade _ordersWindow.Orders has already been changed, but IsNewTrade sees the “old” version of _ordersWindow.Orders , the one that was when CheckNewStrikeA1 was launched

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question