예제 #1
0
        public RaffleViewModel(RaffleService raffleService)
        {
            if (raffleService == null)
            {
                throw new ArgumentNullException("raffleService");
            }

            _raffleService = raffleService;
        }
예제 #2
0
        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;
        }
예제 #3
0
        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;
        }
예제 #4
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);
            }
        }