private void AutoUpdate_FormClosed(object sender, FormClosedEventArgs e)
        {
            // stop notification monitor
            ImmediateNotificationRegister <Product> .StopMonitor(warehouse);

            RegularlyNotificationRegister <Product> .StopMonitor(warehouse);
        }
示例#2
0
        private void AutoUpdate_Load(object sender, EventArgs e)
        {
            btnGetData.Enabled = CanRequestNotifications();

            CreateDatabaseIfNotExist();

            // start immediate notification register
            ImmediateNotificationRegister<Product>.StartMonitor(warehouse);
            RegularlyNotificationRegister<Product>.StartMonitor(warehouse);
        }
        private void GetData_Click(object sender, EventArgs e)
        {
            Decimal lowPrice, highPrice;

            if (!Decimal.TryParse(txtLowPrice.Text, out lowPrice))
            {
                lowPrice = 0;
            }

            if (!Decimal.TryParse(txtHighPrice.Text, out highPrice))
            {
                highPrice = Decimal.MaxValue;
            }


            // Create the query.
            iquery = from p in warehouse.Products
                     where p.Price >= lowPrice && p.Price <= highPrice
                     select p;

            if (this.rabtnImUpdate.Checked)
            {
                // If need to update immediately, use ImmediateNotificationRegister to register
                // SqlDependency.
                notification            = new ImmediateNotificationRegister <Product>(warehouse, iquery);
                notification.OnChanged += NotificationOnChanged;
            }
            else
            {
                // We can use RegularlyNotificationRegister to implement update regularly.
                if (Int32.TryParse(this.txtInterval.Text, out interval))
                {
                    regularNotificaton            = new RegularlyNotificationRegister <Product>(warehouse, iquery, interval * 1000);
                    regularNotificaton.OnChanged += NotificationOnChanged;

                    // Only for displaying the progress
                    this.proBar.Value  = 100 / interval;
                    count              = 1;
                    formTimer          = new Timer();
                    formTimer.Interval = 1000;
                    formTimer.Tick    += formTimer_Tick;
                    formTimer.Start();
                }
                else
                {
                    return;
                }
            }

            GetData();

            ChangeButtonState();
        }
        /// <summary>
        /// Stop SqlDependency.
        /// </summary>
        private void StopSqlDependency(object sender, EventArgs e)
        {
            try
            {
                if (notification != null)
                {
                    notification.Dispose();
                    notification = null;
                }

                if (regularNotificaton != null)
                {
                    regularNotificaton.Dispose();
                    regularNotificaton = null;
                }

                if (formTimer != null)
                {
                    formTimer.Stop();
                    formTimer.Dispose();
                    formTimer         = null;
                    this.proBar.Value = 0;
                }

                ChangeButtonState();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message, "Paramter Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    MessageBox.Show(ex.Message + "(" + ex.InnerException.Message + ")", "Failed to Stop SqlDependency", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    MessageBox.Show(ex.Message, "Failed to Stop SqlDependency", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }