Advertisement
Ange1ofD4rkness

ScoreTableDetector2_1v2-Form1-1/24/2013-1010

Jan 24th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10.  
  11. namespace ScoreTableDetector2_1v2
  12. {
  13.     public delegate void Return_CallBack(string downloadedData);
  14.     public delegate void Return_FormData(List<teamDataItem> tempData, string rtbData);
  15.     public delegate void SetRTBCallback(string text);
  16.     public delegate void SetDGV_New_Callback(teamDataItem tempEntry);
  17.     public delegate void SetDGV_Edit_Callback(teamDataItem tempEntry);
  18. //===================================================================================================================
  19.     public partial class Form1 : Form
  20.     {
  21.         //!!!! - NEED to dispose of the thread and its resources when it's finished, primarily speaking of the delegate called threads for the form and parsing data.
  22.  
  23.  
  24.         private Object thisLock = new Object();
  25.         private Object lockFormUpdate = new Object();
  26.  
  27.         private Thread updateForm_Thread = null;
  28.         private Thread updateForm_Thread_num2 = null;
  29.  
  30.         List<teamDataItem> teamData;
  31.  
  32.         List<string> pointFields;
  33.         List<string> flagNames;
  34.  
  35.         Thread readInThread = null;
  36.         string url;
  37.         int elapsedTime;
  38.  
  39.         readInWebpage readIn;
  40.  
  41.         string downloadedData;
  42.         List<teamDataItem> tempData;
  43.         string rtbData;
  44. //-------------------------------------------------------------------------------------------------------------------
  45.         public Form1 ()
  46.         {
  47.             InitializeComponent();
  48.  
  49.             teamData = new List<teamDataItem>();
  50.  
  51.             pointFields = new List<string>() { };
  52.             flagNames = new List<string>() { };
  53.  
  54.             url = "http://www.reddit.com/new/";
  55.             elapsedTime = 15;
  56.  
  57.             readIn = new readInWebpage(url, "", elapsedTime, new Return_CallBack(updateForm));
  58.  
  59.             button2.Enabled = false;
  60.  
  61.             //setupDataGridView.setup(ref pointFields, ref flagNames, ref dataGridView1); //used to set up the columns
  62.  
  63.             downloadedData = "";
  64.             tempData = new List<teamDataItem>();
  65.             rtbData = "";
  66.  
  67.             this.readInThread = new Thread(readIn.runScan);
  68.             this.updateForm_Thread = new Thread(() => parseData.strip(teamData, downloadedData, pointFields, flagNames, new Return_FormData(updateForm_Data))); //parse the data
  69.             this.updateForm_Thread_num2 = new Thread(() => thread_UpdateFormSafe(tempData, rtbData)); //used to actually apply the updated data to the form
  70.         }
  71. //-------------------------------------------------------------------------------------------------------------------
  72.         private void button1_Click (object sender, EventArgs e) //start
  73.         {
  74.             readIn.stopRun = false;
  75.             checkWebsite();
  76.         }
  77. //-------------------------------------------------------------------------------------------------------------------
  78.         private void button2_Click (object sender, EventArgs e) //stop
  79.         {
  80.             //readInThread.Abort();
  81.             readIn.stopRun = true;
  82.  
  83.             button2.Enabled = false; //change over the state of the buttons to prevent double click issues
  84.             button1.Enabled = true;
  85.         }
  86. //-------------------------------------------------------------------------------------------------------------------
  87.         private void checkWebsite () //used to start the thread
  88.         {
  89.             //this.readInThread = new Thread(readIn.runScan);
  90.             readInThread.Start();
  91.  
  92.             button2.Enabled = true; //change over the state of the buttons to prevent double click issues
  93.             button1.Enabled = false;
  94.         }
  95. //-------------------------------------------------------------------------------------------------------------------
  96.         private void updateForm (string downloadedData) //the delegate class to call back to
  97.         {
  98.             lock (thisLock) //lock (mutex style)
  99.             {
  100.                 this.downloadedData = downloadedData;
  101.  
  102.                 //this.updateForm_Thread = new Thread(() => parseData.strip(teamData, downloadedData, pointFields, flagNames, new Return_FormData(updateForm_Data))); //parse the data
  103.                 this.updateForm_Thread.Start();
  104.  
  105.                 //!!! when the updateForm_Data() function is called, this needs to be disposed of
  106.             }
  107.         }
  108. //-------------------------------------------------------------------------------------------------------------------
  109.         private void updateForm_Data (List<teamDataItem> tempData, string rtbData) //call back by the parseData
  110.         {
  111.             lock (lockFormUpdate) //lock off the form update to one class
  112.             {
  113.                 this.tempData = tempData;
  114.                 this.rtbData = rtbData;
  115.  
  116.                 //this.updateForm_Thread_num2 = new Thread(() => thread_UpdateFormSafe(tempData, rtbData)); //used to actually apply the updated data to the form
  117.                 this.updateForm_Thread_num2.Start();
  118.  
  119.                 //!!! once the form has been updated this needs to be disposed of
  120.             }
  121.         }
  122. //-------------------------------------------------------------------------------------------------------------------
  123.         private void thread_UpdateFormSafe (List<teamDataItem> tempData, string rtbData) //used to update the form data
  124.         {
  125.             //!!!!! I NEVER UPDATE THE COMBOBOX (needs to be done through new entry only)
  126.  
  127.             //if index == -1, and newEntry == true, then it's a new entry
  128.             //if index != -1, and newEntry == false, then it's an old entry who's value changed
  129.             //if index == -1, and newEntry == false, then it's an old entry that was unchanged
  130.  
  131.  
  132.             for (int i = 0; i < tempData.Count; i++) //go through the list
  133.             {
  134.                 if (tempData [i].newEntry == true) //if a new entry was added
  135.                 {
  136.                     //this.addToDataGridView(tempData [i]);
  137.                 }
  138.                 else
  139.                 {
  140.                     if (tempData [i].index != -1) //an existing entry was updates
  141.                     {
  142.                         //this.editDataGridView(tempData [i]);
  143.                     }
  144.                 }
  145.             }
  146.  
  147.             //!!!! ADD - the richTextBox display
  148.         }
  149. //-------------------------------------------------------------------------------------------------------------------
  150.         private void addToDataGridView (teamDataItem tempEntry) //updates dataGridView1 by adding a new item (in thread safe)
  151.         {
  152.             if (this.dataGridView1.InvokeRequired) //if the calling thread is different then the one that created dataGridView1
  153.             {
  154.                 SetDGV_New_Callback dgvCall = new SetDGV_New_Callback(addToDataGridView);
  155.                 this.Invoke(dgvCall, new object [] { tempEntry });
  156.             }
  157.             else //if the same thread that created dataGridView1
  158.             {
  159.                 dataGridView1.Rows.Add();
  160.                 dataGridView1.Rows [dataGridView1.RowCount - 1].HeaderCell.Value = tempEntry.teamName;
  161.  
  162.                 for (int i = 0; i < pointFields.Count; i++) //adds the point fields
  163.                 {
  164.                     dataGridView1 [i, (dataGridView1.Rows.Count - 1)].Value = tempEntry.points [i];
  165.                 }
  166.                 for (int i = 0; i < flagNames.Count; i++) //adds the flag fields
  167.                 {
  168.                     dataGridView1 [(pointFields.Count + i), (dataGridView1.Rows.Count - 1)].Value = tempEntry.flags [i]; //remember it must pass by the pointFields first
  169.                 }
  170.             }
  171.         }
  172. //-------------------------------------------------------------------------------------------------------------------
  173.         private void editDataGridView (teamDataItem tempEntry) //updates dataGridView1 by editing an existing line (in thread safe)
  174.         {
  175.             if (this.dataGridView1.InvokeRequired) //if the calling thread is different then the one that created dataGridView1
  176.             {
  177.                 SetDGV_Edit_Callback dgvCall = new SetDGV_Edit_Callback(editDataGridView);
  178.                 this.Invoke(dgvCall, new object [] { tempEntry });
  179.             }
  180.             else //if the same thread that created dataGridView1
  181.             {
  182.                 int indexFound = -1;
  183.                 for (int i = 0; i < dataGridView1.RowCount; i++) //looks for the index
  184.                 {
  185.                     if (tempEntry.teamName == dataGridView1.Rows [i].HeaderCell.Value.ToString()) //found the same team
  186.                     {
  187.                         indexFound = i;
  188.                         break;
  189.                     }
  190.                 }
  191.  
  192.                 if (indexFound != -1) //we found an entry
  193.                 {
  194.                     for (int i = 0; i < pointFields.Count; i++) //adds the point fields
  195.                     {
  196.                         dataGridView1 [i, indexFound].Value = tempEntry.points [i];
  197.                     }
  198.                     for (int i = 0; i < flagNames.Count; i++) //adds the flag fields
  199.                     {
  200.                         dataGridView1 [(pointFields.Count + i), indexFound].Value = tempEntry.flags [i]; //remember it must pass by the pointFields first
  201.                     }
  202.                 }
  203.                
  204.                 //!!! No else in case a fluke happens and we need to add a new row instead (implement later)
  205.             }
  206.         }
  207. //-------------------------------------------------------------------------------------------------------------------
  208.     }
  209. //===================================================================================================================
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement