public void StartBuyOperation() { foreach (ProductInfo product in toBuy.Keys) { Message msg = messageFactory.BuildOfferRequest(product); Console.WriteLine("Agent {0} send request for {1}", AgentId, product); sendMessage(msg); //Lambda in loop cannot use loop variable like product, it have to be iteration scope variable. ProductInfo _product = product; int quantity = toBuy[product]; Thread t = new Thread(() => { //Now wait fror answers and check offers Thread.Sleep(3000); Console.WriteLine("Check offers for product " + _product); List <OfferAnswerMessage> _offers = null; lock (offers) { _offers = offers[_product]; offers.Remove(_product); } if (_offers == null || _offers.Count == 0) { return; } _offers.Sort((o1, o2) => { return(Math.Sign(o1.Price - o2.Price)); }); OfferAnswerMessage offer = _offers[0]; //We should check the quantity ;-) Message sellReq = messageFactory.BuildSellRequest(offer, quantity); sendMessage(sellReq); }); t.Start(); } }
private void newOfferAnswer(OfferAnswerMessage msg) { Console.WriteLine("Agent {0}, has offer {1}, {2}, {3}", AgentId, msg.ProductInfo, msg.Price, msg.QuantityAviable); lock (offers) { if (!offers.ContainsKey(msg.ProductInfo)) { offers[msg.ProductInfo] = new List <OfferAnswerMessage>(); } offers[msg.ProductInfo].Add(msg); } }