/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonCardSetLoad_Click(object sender, EventArgs e) { OpenFileDialog load = new OpenFileDialog(); if (load.ShowDialog() == DialogResult.OK) { // Attempt to load the last cardset saved try { //Open the file written above and read values from it. Stream stream = File.Open(load.FileName, FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); TempSet = (MTG.MTGCardSet)bformatter.Deserialize(stream); stream.Close(); dataGridViewResults.DataSource = TempSet.CardSet; UpdateGrid(); } catch (Exception ex) { if (!ex.Message.StartsWith("Could not find file")) { MessageBox.Show(String.Format(" **ERROR[LoadTempData]: {0}", ex.Message)); } } } }
/// <summary> /// /// </summary> public MTGClientForm() { InitializeComponent(); // init the collection data Collection = new MTGCollection(); // setup the collection data grid view SetupDataGridView(); // Welcome the player UpdateStatusStrip("Welcome to Magic the Gathering Online!"); // Disable all the extra tabs until someone is logged in EnableTabPages(false); // Load the picture data set CardSets = new ArrayList(); try { // Attempt to load the last cardset saved Stream stream = File.Open("10E.dat", FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); MTGCardSet cards = new MTGCardSet(); cards = (MTGCardSet)bformatter.Deserialize(stream); stream.Close(); CardSets.Add(cards); } catch (Exception ex) { if (!ex.Message.StartsWith("Could not find file")) { LogError(String.Format("Init: ", ex.Message)); } else { LogError("Cannot find the cards data pack."); } } WaitingData = new byte[PacketSize]; }
/// <summary> /// Initialize the form /// </summary> public Form1() { InitializeComponent(); // init config vars textBoxFetchPagesInParallel.Text = Properties.Settings.Default.PagesToFetchInParallel; // setup the datagridview results panel SetupDataGridView(); // init temp storage TempSet = new MTG.MTGCardSet(); TempIDs = new ArrayList(); LoadTempData(); // setup the link in the settings tab... mmb - is this needed? linkLabelDefault.Links.Add(0, 33, "http://ww2.wizards.com/gatherer/"); // choose which magic color to view // mmb - is this needed any more? // choose which Card Sets to fetch comboBoxFetchMTGEdition.Items.Add(MTGEditions.FourthE.ToString()); comboBoxFetchMTGEdition.Items.Add(MTGEditions.FifthE.ToString()); comboBoxFetchMTGEdition.Items.Add(MTGEditions.TenthE.ToString()); comboBoxFetchMTGEdition.SelectedIndex = 2; // mmb - debugging only // Create a background worker thread that fetches data from the web site asnyc'ly and Reports Progress and Supports Cancellation FetchAsyncWorker.WorkerReportsProgress = true; FetchAsyncWorker.WorkerSupportsCancellation = true; FetchAsyncWorker.ProgressChanged += new ProgressChangedEventHandler(FetchAsync_ProgressChanged); // can one of these be made for all controls so invoke won't have to be done? FetchAsyncWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(FetchAsync_RunWorkerCompleted); FetchAsyncWorker.DoWork += new DoWorkEventHandler(FetchAsync_DoWork); // Silently suppress P/Invoke errors if not running on XP/Vista... try { SetWindowTheme(progressBar1.Handle, "", ""); } catch { } }
/// <summary> /// Load the serialized data /// </summary> private void LoadTempData() { // Attempt to load the last cardset saved try { //Open the file written above and read values from it. Stream stream = File.Open("cardset.dat", FileMode.Open); BinaryFormatter bformatter = new BinaryFormatter(); TempSet = (MTG.MTGCardSet)bformatter.Deserialize(stream); stream.Close(); dataGridViewResults.DataSource = TempSet.CardSet; UpdateGrid(); } catch (Exception ex) { if (!ex.Message.StartsWith("Could not find file")) { MessageBox.Show(String.Format(" **ERROR[LoadTempData]: {0}", ex.Message)); } } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FetchAsync_DoWork(object sender, DoWorkEventArgs e) { try { // The sender is the BackgroundWorker object we need it to // report progress and check for cancellation. BackgroundWorker bwAsync = sender as BackgroundWorker; // start the wait cursor WaitCursor(true); TempSet = new MTG.MTGCardSet(); String SelectedEdition = GetComboBoxValue(); if (GetComboBoxValue() == MTGEditions.FourthE.ToString().ToLower()) { SelectedEdition = "Fourth%20Edition"; } else if (GetComboBoxValue() == MTGEditions.TenthE.ToString().ToLower()) { SelectedEdition = "Tenth%20Edition"; } // create the web page to search... // example: // http://ww2.wizards.com/gatherer/index.aspx?term=&Field_Name=on&Field_Rules=on&Field_Type=on&setfilter=Fourth%20Edition String InitialWebPage = "http://ww2.wizards.com/gatherer/index.aspx"; InitialWebPage += "?term=&Field_Name=on&Field_Rules=on&Field_Type=on&setfilter=" + SelectedEdition; // Store the temp data that will be sent to the web page parser TempData temp = new TempData(); temp._webpage = InitialWebPage; temp._mtgedition = SelectedEdition; // Now pull back all the card IDs WebPageParser.FetchInitialPage(temp); // update progression bwAsync.ReportProgress(0); // create a var for all the threads Thread[] WebFetchingThreads = new Thread[TempIDs.Count]; Int32 Page = 0; Int32 PagesCompleted = 0; // cycle through the pages to get all the scores foreach (Int32 id in TempIDs) { // update progression bwAsync.ReportProgress(Convert.ToInt32(Page * (100.0 / TempIDs.Count))); // create the web page to search... // example: // http://ww2.wizards.com/gatherer/CardDetails.aspx?&id=2085 String CardWebPage = "http://ww2.wizards.com/gatherer/CardDetails.aspx?&id="; // just fill these in for now CardWebPage += id.ToString(); // The constructor for the Thread class requires a ThreadStart // delegate that represents the method to be executed on the temp = new TempData(); temp._webpage = CardWebPage; temp._mtgedition = SelectedEdition; // Create the thread object, passing in the serverObject.StaticMethod method // using a ThreadStart delegate. WebFetchingThreads[Page] = new Thread(WebPageParser.FetchCardPage); // Start the thread. // Note that on a uniprocessor, the new thread does not get any processor // time until the main thread is preempted or yields. WebFetchingThreads[Page].Start(temp); // Periodically check if a cancellation request is pending. // If the user clicks cancel the line // m_AsyncWorker.CancelAsync(); if ran above. This // sets the CancellationPending to true. // You must check this flag in here and react to it. // We react to it by setting e.Cancel to true and leaving. if (bwAsync.CancellationPending) { // stop the job... //Log("Searching stopped!"); break; } // Only allow so many pages to be fetched in parallel if ((Page - PagesCompleted) >= (MaxPagesToFetchInParallel - 1)) { // wait for the first run thread to return before letting another one start WebFetchingThreads[Page].Join(); PagesCompleted++; // update progression bwAsync.ReportProgress(Convert.ToInt32((PagesCompleted) * (100.0 / TempIDs.Count))); } Page++; } // now wait and make sure that all web pages have returned back foreach (Thread t in WebFetchingThreads) { t.Join(); } bwAsync.ReportProgress(100); } catch (Exception ex) { //Log(String.Format(" **ERROR: ", ex.Message)); MessageBox.Show(String.Format(" **ERROR[FetchAsync_DoWork]: {0}", ex.Message)); } // stop the wait cursor WaitCursor(false); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridViewCollection_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { this.Cursor = Cursors.WaitCursor; // sort by column header MTGCardSet Temp = new MTGCardSet(); Temp.CardSet = (ArrayList)dataGridViewCollection.DataSource; Temp.Sort(e.ColumnIndex); dataGridViewCollection.DataSource = Temp.CardSet; UpdateCollectionGrid(); this.Cursor = Cursors.Default; }