private void Initialize() { //statest.Cells[0].Value = "Hello"; //ForexMarketUpdatesTable.Rows. currencychart = CurrencyGraph; table = ForexMarketUpdatesTable; InitializeForexTable(table); UpdateTableBackgroundWorker.RunWorkerAsync(); UpdateChartBackgroundWorker.RunWorkerAsync(); UpdateCSVFileBackgroundWorker.RunWorkerAsync(); TrueFX.initialize(); }
// This method is linked with the UpdateTableBackgroundWorker_DoWork private void UpdateTableBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { //Equivalent to about 5 seconds int frequencyinmilliseconds = 5000; String[] information; String bidbigfigurestring; String bidpointsstring; String offerbigfigurestring; String offerpointsstring; String currency; String millisecondtimestamp; String high; String low; String open; while (true) { // // // // // This is the past mode portion of the piece // // // // // This past mode isn't developed for the async if (TrueFX.getpastmodestate() == true) { foreach (String currencyname in requestedcurrencies) { //This while loop is gonna be a problem for past mode //Because we dont exactly need the market to be open in order for //Past mode to be enabled, but here it does. Same thing for the //The UpdaetChart method below too. Have to fix that. //This logic will run for a bit, but it will crash eventually. You need to //add a method that will peek to see if there is any more data to log. while (((TrueFX.getinformationbatch() == /*null*/ Environment.NewLine) || (TrueFX.getinformationbatch() == null)) && (TrueFX.getpastmodestate() == false)) { } if (TrueFX.getpastmodestate() == false) { break; } information = TrueFX.RequestCurrency(currencyname); //currency = information[0].Remove(information[0].IndexOf('\n'), information[0].IndexOf('\n') + 1); currency = information[0]; millisecondtimestamp = information[1]; bidbigfigurestring = information[2]; bidpointsstring = information[3]; offerbigfigurestring = information[4]; offerpointsstring = information[5]; high = information[6]; low = information[7]; open = information[8]; tabledata[currencyname] = information; table.Invoke(tableagentaddrow, currency, millisecondtimestamp, bidbigfigurestring + bidpointsstring, offerbigfigurestring + offerpointsstring, high, low, open); if (ChartTableSyncCheckBox.Checked == true) { if (UpdateChartBackgroundWorker.IsBusy == true) { UpdateChartBackgroundWorker.CancelAsync(); break; } else { foreach (String currencychecked in requestedcurrencies) { //There really should only be one string in requested currencies in //light of the whole only able to chose one currecny at a time from //the box list on the actual form so. // while (((TrueFX.getinformationbatch() == /*null*/ Environment.NewLine) || (TrueFX.getinformationbatch() == null)) && (TrueFX.getpastmodestate() == false)) { } information = tabledata[currencychecked]; //The zero thing should work for the commented reason above //Console.WriteLine("ENETERED THE THREAD"); // BUG SQUASHING COMMENT // The program doesn't enter the the loop if currency to be logged is checked. // That must mean requestedcurrencies is empty, when we check the currecncies to be logged check box. currencychart.Invoke(chartagentplotpoint, information, 0); } } } else { UpdateChartBackgroundWorker.RunWorkerAsync(); } //This should still work because theoretically only //One currency is checked at a time still so //currencychart.Invoke(chartagentplotpoint, information, 0); } } //Whats happening is past mode is getting enabled in the middle of the else loop, //Forcing thre rest of the currencies to do the requested thing for all currencies //But you might not have the data for it. Ill have to fix this. else { foreach (String currencyname in ALLCURRENCIES) { //This while loop is gonna be a problem for past mode //Because we dont exactly need the market to be open in order for //Past mode to be enabled, but here it does. Same thing for the //The UpdaetChart method below too. Have to fix that. while (((TrueFX.getinformationbatch() == /*null*/ Environment.NewLine) || (TrueFX.getinformationbatch() == null)) && (TrueFX.getpastmodestate() == false)) { } if (TrueFX.getpastmodestate()) { break; } information = TrueFX.RequestCurrency(currencyname); //currency = information[0].Remove(information[0].IndexOf('\n'), information[0].IndexOf('\n') + 1); currency = information[0]; millisecondtimestamp = information[1]; bidbigfigurestring = information[2]; bidpointsstring = information[3]; offerbigfigurestring = information[4]; offerpointsstring = information[5]; high = information[6]; low = information[7]; open = information[8]; tabledata[currencyname] = information; table.Invoke(tableagentaddrow, currency, millisecondtimestamp, bidbigfigurestring + bidpointsstring, offerbigfigurestring + offerpointsstring, high, low, open); //This should still work because theoretically only //One currency is checked at a time still so //currencychart.Invoke(chartagentplotpoint, information, 0); if (ChartTableSyncCheckBox.Checked == true) { if (UpdateChartBackgroundWorker.IsBusy == true) { Console.WriteLine("ITS CANCELING THE UPDATE BACKGROUND"); UpdateChartBackgroundWorker.CancelAsync(); break; } else { foreach (String currencychecked in requestedcurrencies) { //There really should only be one string in requested currencies in //light of the whole only able to chose one currecny at a time from //the box list on the actual form so. information = tabledata[currencychecked]; //The zero thing should work for the commented reason above //Console.WriteLine("ENETERED THE THREAD"); // BUG SQUASHING COMMENT // The program doesn't enter the the loop if currency to be logged is checked. // That must mean requestedcurrencies is empty, when we check the currecncies to be logged check box. if (UpdateChartBackgroundWorker.IsBusy) { Console.WriteLine("WHY IS IT BUSY?"); } currencychart.Invoke(chartagentplotpoint, information, 0); } } } else { // There are still errors here. I think whats happening is that we call for the cancellation of work, and we // don't wait for the updatechartbackgroundworker to return befor calling the run asynchronus method. Console.WriteLine("The extra special async cancel"); UpdateChartBackgroundWorker.CancelAsync(); while (UpdateChartBackgroundWorker.IsBusy) { } UpdateChartBackgroundWorker.RunWorkerAsync(); } } } System.Threading.Thread.Sleep(frequencyinmilliseconds); //This method really isn't very visually pleasing //table.Invoke( tableagentclearrow ); } }