public void ExecuteAuctionRound() { List <Task> tasks = new List <Task>(); List <Raiser> raisers = new List <Raiser>(); object _lock = new object(); if (UpdateAuction != null) { Parallel.ForEach(UpdateAuction.GetInvocationList(), item => { tasks.Add(Task.Factory.StartNew(() => { lock (_lock) raisers.Add((Raiser)item.DynamicInvoke(Price, JumpOfPrice)); })); }); } Task.WaitAll(tasks.ToArray()); if (raisers != null) { var check = raisers.OrderByDescending(item => item.RaisePrice).First(); if (check.RaisePrice > Price) { Price = check.RaisePrice; MaxRaiser = check; } } }
public int RunAuction() { System.Threading.Thread.Sleep(1000); PrintAuctionStart(); IsOpen = true; aTimer = new Timer(10000); bool cancel = false; aTimer.Elapsed += (s, e) => { cancel = true; aTimer.Stop(); aTimer.Enabled = false; }; aTimer.Start(); while (!cancel) { if (UpdateAuction != null) { if (UpdateAuction.GetInvocationList().Length > 1) { ExecuteAuctionRound(); UnSubscribe(); Price = MaxRaiser.RaisePrice; if (UpdateAuction != null) { if (UpdateAuction.GetInvocationList().Length > 1) { aTimer.Stop(); aTimer.Close(); //aTimer.Dispose(); //aTimer = new Timer(10000); aTimer.Interval = 1000; aTimer.Start(); } else { cancel = true; } } else { cancel = true; } } } else { cancel = true; } } aTimer.Stop(); aTimer.Enabled = false; IsOpen = false; aTimer.Dispose(); PrintAuctionEnd(); if (MaxRaiser.Agent != null) { MaxRaiser.Agent.Money -= MaxRaiser.RaisePrice; return(Price); } else { return(0); } }