コード例 #1
0
        /// <summary>
        /// Background thread function for stock article update. Gets called when
        /// background thread worker gets run asynchronously.
        /// </summary>
        /// <param name="sender">object that sent the function</param>
        /// <param name="e">arguments (parameters & results etc.)</param>
        private void workerArticleUpdate_DoWork(object sender, DoWorkEventArgs e)
        {
            cArticleScanner  scanner = (cArticleScanner)e.Argument;
            BackgroundWorker bw      = sender as BackgroundWorker;

            e.Result = scanner.getLatestArticles(bw);
        }
コード例 #2
0
        /// <summary>
        /// This functions handles the Check New Articles button when clicked from
        /// the main screen. It will attempt to query the most recent articles on
        /// investopedia.com and record those articles if they are more recent than
        /// the last recorded article insertion.
        /// </summary>
        /// <param name="sender">Default parameter passed by c# win forms</param>
        /// <param name="e">Default parameter passed by win forms</param>
        private void btnCheckNewArticles_Click(object sender, EventArgs e)
        {
            btnCheckNewArticles.Enabled = false;
            btnUpdateStocks.Enabled     = false;
            btn_analyzeArticles.Enabled = false;

            BackgroundWorker workerArticleUpdate = new BackgroundWorker();

            workerArticleUpdate.DoWork               += new DoWorkEventHandler(workerArticleUpdate_DoWork);
            workerArticleUpdate.RunWorkerCompleted   += new RunWorkerCompletedEventHandler(workerArticleUpdate_RunWorkerCompleted);
            workerArticleUpdate.WorkerReportsProgress = true;
            workerArticleUpdate.ProgressChanged      += new ProgressChangedEventHandler(workerArticleUpdate_ProgressChanged);

            //before kicking off the background worker, check that there are articles to be scraped first
            cArticleScanner scanner      = new cArticleScanner();
            int             new_articles = scanner.checkForNewArticles();

            //if new articles exist, then begin the background worker
            if (new_articles == 1)
            {
                MessageBox.Show("New articles found!");
                toolStripStatusLabel.Text = "Updating articles...";
                workerArticleUpdate.RunWorkerAsync(scanner);
            }
            else if (new_articles == 0)             //no new articles? re-enable all buttons
            {
                MessageBox.Show("No new articles!");
                btnCheckNewArticles.Enabled = true;
                btnUpdateStocks.Enabled     = true;
                btn_analyzeArticles.Enabled = true;
            }
            else if (new_articles == -1)                //no articles found in database at all?
            {
                //warn user not to select articles dated too far in the past
                MessageBox.Show("No articles were found in database. Please input date you would like to scrape articles to (it is NOT recommended to scan articles from as far back as possible[2004] for performance reasons. Please select a date around when you began tracking stock prices, otherwise pick a date no more than 5 days in the past)");

                //set oldest date for picker box to 2004 & latest date to yesterday
                var picker = new DateTimePicker();
                picker.MinDate = new DateTime(2004, 1, 1);
                picker.MaxDate = DateTime.Now.AddDays(-1); //yesterday
                picker.Left    = 11;                       //center the date thing
                picker.Top     = 6;

                fDatePicker f = new fDatePicker();      //create date picker form (basically empty)
                f.Controls.Add(picker);                 //add control to form

                var result = f.ShowDialog();
                if (result == DialogResult.OK)
                {
                    //get selected date & set it as last article update (so scanner will grab articles until that date is reached)
                    DateTime selected_date = picker.Value;
                    Console.WriteLine(selected_date.ToString());
                    scanner.setLastArticleDate(selected_date);
                    toolStripStatusLabel.Text = "Updating articles...";
                    workerArticleUpdate.RunWorkerAsync(scanner);                        //pop off background worker with new date to scan until
                }
            }
        }