public RaffleViewModel(RaffleService raffleService) { if (raffleService == null) { throw new ArgumentNullException("raffleService"); } _raffleService = raffleService; }
private void BeginUIWait(PaymentClass paymentClass, CancellationToken token) { //just want dirty numbers var rnd = new System.Random(); var tickets = RaffleService.GetTickets(paymentClass); while (!token.IsCancellationRequested) { FacadeTicket = tickets.ElementAt(rnd.Next(0, tickets.Count())); } FacadeTicket = null; }
public MainWindow() { InitializeComponent(); Raffle = new RaffleService(); Timer = new System.Timers.Timer(); Timer.Interval = 10; Timer.Elapsed += (sender, e) => { _labelTicket.Dispatcher.Invoke(new Action(() => { var ticket = Raffle.GetRandomTicket((PaymentClass)Enum.Parse(typeof(PaymentClass), _comboType.SelectedItem.ToString())); _labelName.Content = ticket.Name; _labelTicket.Content = ticket.TicketCode; DoEvents(); })); }; Timer.Enabled = false; _comboType.ItemsSource = Enum.GetNames(typeof(PaymentClass)); _comboType.SelectedIndex = 0; }
protected virtual void OnDrawRaffleVictor(PaymentClass paymentClass) { try { CanExecuteRaffle = false; WinningTicket = null; CancellationTokenSource cts = new CancellationTokenSource(); //todo: find a better way to handle this asynchronously Task.Run(() => { BeginUIWait(paymentClass, cts.Token); }); Task.Run(async() => { var ticket = RaffleService.GetRandomTicket(paymentClass); await Task.Delay(TimeSpan.FromSeconds(3)); return(ticket); }) .ContinueWith((t) => { cts.Cancel(); WinningTicket = t.Result; CanExecuteRaffle = true; }); } catch (Exception ex) { HandleException?.Invoke(this, ex); } }