/// <summary>
        /// Main button to run the simulation.
        /// </summary>
        private void PriceOptionClick(object sender, RoutedEventArgs e)
        {
            this.cmdPriceOption.IsEnabled = false;

            this.spinnerWait.Visibility = System.Windows.Visibility.Visible;
            this.spinnerWait.Spin       = true;

            var initial  = Convert.ToDouble(txtInitialPrice.Text);
            var exercise = Convert.ToDouble(txtExercisePrice.Text);
            var up       = Convert.ToDouble(txtUpGrowth.Text);
            var down     = Convert.ToDouble(txtDownGrowth.Text);
            var interest = Convert.ToDouble(txtInterestRate.Text);
            var periods  = Convert.ToInt64(txtPeriods.Text);
            var sims     = Convert.ToInt64(txtSimulations.Text);

            //
            // Run simulation to price option:
            //
            string result = string.Empty;

            Task T = new Task(() =>
            {
                var rand  = new Random();
                var start = System.Environment.TickCount;

                var price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);

                var stop = System.Environment.TickCount;

                var elapsedTimeInSecs = (stop - start) / 1000.0;

                result = $"{price:C}  [{elapsedTimeInSecs:#,##0.00} secs]";
            });

            //
            // Display the results:
            //

            Task T2 = T.ContinueWith(antecedent =>
            {
                this.lstPrices.Items.Insert(0, result);

                this.spinnerWait.Spin       = false;
                this.spinnerWait.Visibility = System.Windows.Visibility.Collapsed;

                this.cmdPriceOption.IsEnabled = true;
            }, TaskScheduler.FromCurrentSynchronizationContext());

            T.Start(); // Go!
        }
Пример #2
0
        private void btnOption_Click(object sender, EventArgs e)
        {
            double initial  = Convert.ToDouble(txtInitialPrice.Text);
            double exercise = Convert.ToDouble(txtExcercisePrice.Text);
            double up       = Convert.ToDouble(txtUpGrowth.Text);
            double down     = Convert.ToDouble(txtDownGrowth.Text);
            double interest = Convert.ToDouble(txtInterestRate.Text);
            long   periods  = Convert.ToInt64(txtPeriods.Text);
            long   sims     = Convert.ToInt64(txtSimulations.Text);

            /* First approach
             * //
             * // Run simulation to price option:
             * //
             *
             * Task T = new Task(() =>
             * {
             *
             *     btnOption.Enabled = false;
             *     Cursor.Current = Cursors.WaitCursor;
             *
             *     Random rand = new Random();
             *     int start = System.Environment.TickCount;
             *
             *     double price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);
             *
             *     int stop = System.Environment.TickCount;
             *
             *     double elapsedTimeInSecs = (stop - start) / 1000.0;
             *
             *     string result = string.Format("{0:C}  [{1:#,##0.00} secs]",
             *         price, elapsedTimeInSecs);
             *
             *      //
             *      // Display the results:
             *      //
             *      lstPrices.Items.Add(result);
             *
             *     Cursor.Current = Cursors.Default;
             *     btnOption.Enabled = true;
             *
             * }
             * );
             *
             * // Standar Execution
             * //T.Start(); //Go Task!
             *
             * // Execution passing context
             * T.Start( TaskScheduler.FromCurrentSynchronizationContext() ); //Go Task!
             *
             */

            /* Second approach */
            //
            // Run simulation to price option:
            //
            string result = "";

            taskCounter++;
            lblCounter.Text = "Tasks running: " + taskCounter.ToString();

            //btnOption.Enabled = false;
            Cursor.Current     = Cursors.WaitCursor;
            picSpinner.Visible = true;

            Task T = new Task(() =>
            {
                Random rand = new Random();
                int start   = System.Environment.TickCount;

                double price = AsianOptionsPricing.Simulation(rand, initial, exercise, up, down, interest, periods, sims);

                int stop = System.Environment.TickCount;

                double elapsedTimeInSecs = (stop - start) / 1000.0;

                result = string.Format("{0:C}  [{1:#,##0.00} secs]",
                                       price, elapsedTimeInSecs);
            }
                              );

            Task T2 = T.ContinueWith((antecedent) =>
            {
                //
                // Display the results:
                //
                lstPrices.Items.Add(result);

                taskCounter--;
                lblCounter.Text = "Tasks running: " + taskCounter.ToString();

                if (taskCounter == 0)
                {
                    picSpinner.Visible = false;
                    Cursor.Current     = Cursors.Default;
                    //btnOption.Enabled = true;
                }
            },
                                     TaskScheduler.FromCurrentSynchronizationContext()
                                     );

            // Standar Execution
            T.Start(); //Go Task!
        }