[Quantproject-developers] QuantDownloader/Downloader/OpenTickDownloader/UserForms OTWebDownloader.
Brought to you by:
glauco_1
|
From: Marco M. <mi...@us...> - 2008-08-23 13:09:53
|
Update of /cvsroot/quantproject/QuantDownloader/Downloader/OpenTickDownloader/UserForms In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv15487/Downloader/OpenTickDownloader/UserForms Modified Files: OTWebDownloader.cs Log Message: OTWebDownloader should now repaint itself in a thread-safe manner (during downloading bars) Index: OTWebDownloader.cs =================================================================== RCS file: /cvsroot/quantproject/QuantDownloader/Downloader/OpenTickDownloader/UserForms/OTWebDownloader.cs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** OTWebDownloader.cs 29 Jul 2008 22:38:17 -0000 1.4 --- OTWebDownloader.cs 22 Aug 2008 22:57:43 -0000 1.5 *************** *** 58,61 **** --- 58,65 ---- private string textForStartingDownloadingTimeLabel; private string textForEndingDownloadingTimeLabel; + private string[] tickersToDownload; + private SortedList downloadingTickersSortedList; + private int indexOfCurrentUpdatingTicker; + private DateTime currentUpdatingTickerDateTimeOfLastBarUpdate; public OTWebDownloader(DataTable tableOfSelectedTickers) *************** *** 66,73 **** InitializeComponent(); this.commonInitialization(); - // - //this.Text = "Download quotes from OpenTick for the selected tickers"; this.tableOfSelectedTickers = tableOfSelectedTickers; ! // } --- 70,75 ---- InitializeComponent(); this.commonInitialization(); this.tableOfSelectedTickers = tableOfSelectedTickers; ! this.initializeDownloadingTickersSortedList(); } *************** *** 86,90 **** base.Dispose( disposing ); } - /// <summary> /// common initialization of the controls of the form --- 88,91 ---- *************** *** 97,102 **** --- 98,111 ---- this.dataGrid1.ContextMenu = new TickerViewerMenu(this); this.Closing += new CancelEventHandler(this.OTWebDownloader_Closing); + this.indexOfCurrentUpdatingTicker = -1; } + private void initializeDownloadingTickersSortedList() + { + this.downloadingTickersSortedList = new SortedList(); + for(int i = 0; i<this.tableOfSelectedTickers.Rows.Count; i++) + this.downloadingTickersSortedList.Add( this.tableOfSelectedTickers.Rows[i][0], i ); + } + #region Windows Form Designer generated code /// <summary> *************** *** 137,141 **** this.endingDownloadingTimeLabel = new System.Windows.Forms.Label(); this.startingDownloadingTimeLabel = new System.Windows.Forms.Label(); - this.timer1 = new System.Windows.Forms.Timer(this.components); this.signallingLabel = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); --- 146,149 ---- *************** *** 453,467 **** this.startingDownloadingTimeLabel.Text = "."; // - // timer1 - // - this.timer1.Enabled = true; - this.timer1.Interval = 1000; - this.timer1.Tick += new System.EventHandler(this.Timer1Tick); - // // signallingLabel // this.signallingLabel.Location = new System.Drawing.Point(84, 415); this.signallingLabel.Name = "signallingLabel"; ! this.signallingLabel.Size = new System.Drawing.Size(129, 11); this.signallingLabel.TabIndex = 38; // --- 461,469 ---- this.startingDownloadingTimeLabel.Text = "."; // // signallingLabel // this.signallingLabel.Location = new System.Drawing.Point(84, 415); this.signallingLabel.Name = "signallingLabel"; ! this.signallingLabel.Size = new System.Drawing.Size(274, 12); this.signallingLabel.TabIndex = 38; // *************** *** 499,502 **** --- 501,505 ---- this.Text = "OT Web downloader"; this.Load += new System.EventHandler(this.otWebDownloaderLoad); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.OTWebDownloaderPaint); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBoxWebDownloaderOptions.ResumeLayout(false); *************** *** 510,514 **** } private System.Windows.Forms.Label signallingLabel; - private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Label endingDownloadingTimeLabel; private System.Windows.Forms.Label startingDownloadingTimeLabel; --- 513,516 ---- *************** *** 571,576 **** #endregion - #region download thread - private void buttonDownloadQuotesOfSelectedTickers_Click(object sender, System.EventArgs e) { --- 573,576 ---- *************** *** 592,602 **** this.txtOpenTickPassword.Text); tickerDownloader.DownloadingStarted += ! new DownloadingStartedEventHandler(this.refreshForm_atDownloadedStarted); tickerDownloader.DatabaseUpdated += ! new DatabaseUpdatedEventHandler(this.refreshGrid); tickerDownloader.DownloadingCompleted += ! new DownloadingCompletedEventHandler(this.refreshForm_atDownloadedCompleted); this.buttonDownloadQuotesOfSelectedTickers.Enabled = false; - //tickerDownloader.DownloadTickers(); this.downloadThread = new Thread( tickerDownloader.DownloadTickers ); this.downloadThread.Start(); --- 592,601 ---- this.txtOpenTickPassword.Text); tickerDownloader.DownloadingStarted += ! new DownloadingStartedEventHandler(this.setStartingTime_atDownloadedStarted); tickerDownloader.DatabaseUpdated += ! new DatabaseUpdatedEventHandler(this.refreshIndexAndDateTimeForCurrentUpdatingTicker); tickerDownloader.DownloadingCompleted += ! new DownloadingCompletedEventHandler(this.setEndingTime_atDownloadedCompleted); this.buttonDownloadQuotesOfSelectedTickers.Enabled = false; this.downloadThread = new Thread( tickerDownloader.DownloadTickers ); this.downloadThread.Start(); *************** *** 608,640 **** } ! #endregion ! ! private void refreshGrid(object sender, DatabaseUpdatedEventArgs eventArgs) { ! DataTable tickersCurrentylDownloaded = ! this.DsTickerCurrentlyDownloaded.Tables["Tickers"]; ! for(int i = 0; i < tickersCurrentylDownloaded.Rows.Count; i++) { ! if( (string)tickersCurrentylDownloaded.Rows[i][0] == eventArgs.Ticker ) ! { ! tickersCurrentylDownloaded.Rows[i][1] = "Yes"; ! tickersCurrentylDownloaded.Rows[i][2] = eventArgs.DateTimeOfLastBarUpdated; ! } ! } } ! private void refreshForm_atDownloadedStarted(object sender, DownloadingStartedEventArgs eventArgs) { ! this.textForStartingDownloadingTimeLabel = ! "Downloading started at: " + eventArgs.StartingDateTime.ToString(); ! this.downloadingInProgress = true; } ! private void refreshForm_atDownloadedCompleted(object sender, DownloadingCompletedEventArgs eventArgs) { ! this.textForEndingDownloadingTimeLabel = "Downloading completed at: " + ! eventArgs.EndingDateTime.ToString(); ! this.downloadingInProgress = false; } ! #region properties --- 607,651 ---- } ! #region Form's methods called by the thread started at OTTickerDownloader's method ! ! private void refreshIndexAndDateTimeForCurrentUpdatingTicker(object sender, DatabaseUpdatedEventArgs eventArgs) { ! lock(this.downloadingTickersSortedList) { ! this.currentUpdatingTickerDateTimeOfLastBarUpdate = ! eventArgs.DateTimeOfLastBarUpdated; ! int sortedListIndexOfKey = ! this.downloadingTickersSortedList.IndexOfKey(eventArgs.Ticker); ! this.indexOfCurrentUpdatingTicker = ! (int)this.downloadingTickersSortedList.GetByIndex(sortedListIndexOfKey); ! this.downloadingInProgress = true; //if a database ! //update event has been risen, downloading has to be ! //in progress ! this.Invalidate();//this forces the form to repaint itself ! //in a thread - safe manner ! } } ! private void setStartingTime_atDownloadedStarted(object sender, DownloadingStartedEventArgs eventArgs) { ! lock(this.startingDownloadingTimeLabel) ! { ! this.textForStartingDownloadingTimeLabel = ! "Downloading started at: " + eventArgs.StartingDateTime.ToString(); ! this.downloadingInProgress = true; ! } } ! private void setEndingTime_atDownloadedCompleted(object sender, DownloadingCompletedEventArgs eventArgs) { ! lock(this.endingDownloadingTimeLabel) ! { ! this.textForEndingDownloadingTimeLabel = "Downloading completed at: " + ! eventArgs.EndingDateTime.ToString(); ! this.downloadingInProgress = false; ! } } ! ! #endregion ! #region properties *************** *** 669,678 **** get { ! string[] tickersToDownload = new string[this.tableOfSelectedTickers.Rows.Count]; ! for(int i = 0; i < tickersToDownload.Length; i++) ! tickersToDownload[i] = ! (string)this.tableOfSelectedTickers.Rows[i][0]; ! ! return tickersToDownload; } } --- 680,692 ---- get { ! if(this.tickersToDownload == null) ! { ! this.tickersToDownload = ! new string[this.tableOfSelectedTickers.Rows.Count]; ! for(int i = 0; i < this.tickersToDownload.Length; i++) ! this.tickersToDownload[i] = ! (string)this.tableOfSelectedTickers.Rows[i][0]; ! } ! return this.tickersToDownload; } } *************** *** 753,762 **** this.otWebDownloaderLoad_fillDataGridWithTickersToBeDownloaded(); } ! private void Timer1Tick_refreshSignallingLabel() { if(this.downloadingInProgress) { ! if(this.signallingLabel.Text.Length<40) this.signallingLabel.Text += "--"; else --- 767,789 ---- this.otWebDownloaderLoad_fillDataGridWithTickersToBeDownloaded(); } + + private void OTWebDownloader_Closing(Object sender, CancelEventArgs e) + { + if (this.downloadingInProgress) + { + e.Cancel = true; + MessageBox.Show("You can't close the form if downloading is still in progress!"); + } + else + { + e.Cancel = false; + } + } ! private void OTWebDownloaderPaint_refreshSignallingLabel() { if(this.downloadingInProgress) { ! if(this.signallingLabel.Text.Length<60) this.signallingLabel.Text += "--"; else *************** *** 769,791 **** } ! void Timer1Tick(object sender, EventArgs e) { ! this.Timer1Tick_refreshSignallingLabel(); ! this.startingDownloadingTimeLabel.Text = textForStartingDownloadingTimeLabel; ! this.endingDownloadingTimeLabel.Text = textForEndingDownloadingTimeLabel; ! } ! private void OTWebDownloader_Closing(Object sender, CancelEventArgs e) { ! if (this.downloadingInProgress) ! { ! e.Cancel = true; ! MessageBox.Show("You can't close the form if downloading is still in progress!"); ! } ! else ! { ! e.Cancel = false; ! } ! } } } --- 796,845 ---- } ! private void OTWebDownloaderPaint_refreshTableOfSelectedTickers() { ! lock(this.downloadingTickersSortedList) ! { ! DataTable tickersCurrentylDownloaded = ! this.DsTickerCurrentlyDownloaded.Tables["Tickers"]; ! if(this.indexOfCurrentUpdatingTicker != -1) ! //a databaseUpdated event has been risen ! { ! tickersCurrentylDownloaded.Rows[this.indexOfCurrentUpdatingTicker][1] = "Yes"; ! tickersCurrentylDownloaded.Rows[this.indexOfCurrentUpdatingTicker][2] = ! this.currentUpdatingTickerDateTimeOfLastBarUpdate; ! } ! } ! } ! private void OTWebDownloaderPaint_refreshEndingDownloadingTimeLabel() { ! lock(this.endingDownloadingTimeLabel) ! { ! this.endingDownloadingTimeLabel.Text = ! this.textForEndingDownloadingTimeLabel; ! } ! } ! ! private void OTWebDownloaderPaint_refreshStartingDownloadingTimeLabel() ! { ! lock(this.startingDownloadingTimeLabel) ! { ! this.startingDownloadingTimeLabel.Text = ! this.textForStartingDownloadingTimeLabel; ! } ! } ! ! //this event handler should be called only by the ! //thread that owns the form. With these locks, ! //information coming from the thread that owns the ! //OT ticker downloader should be "treated" ! //(written and displayed by the form) in a thread-safe manner ! void OTWebDownloaderPaint(object sender, PaintEventArgs e) ! { ! this.OTWebDownloaderPaint_refreshStartingDownloadingTimeLabel(); ! this.OTWebDownloaderPaint_refreshSignallingLabel(); ! this.OTWebDownloaderPaint_refreshTableOfSelectedTickers(); ! this.OTWebDownloaderPaint_refreshEndingDownloadingTimeLabel(); ! } } } |