Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace ScoreTableDetector2_1v2
- {
- public delegate void Return_CallBack(string downloadedData);
- public delegate void Return_FormData(List<teamDataItem> tempData, string rtbData);
- public delegate void SetRTBCallback(string text);
- public delegate void SetDGV_New_Callback(teamDataItem tempEntry);
- public delegate void SetDGV_Edit_Callback(teamDataItem tempEntry);
- //===================================================================================================================
- public partial class Form1 : Form
- {
- //!!!! - 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.
- private Object thisLock = new Object();
- private Object lockFormUpdate = new Object();
- private Thread updateForm_Thread = null;
- private Thread updateForm_Thread_num2 = null;
- List<teamDataItem> teamData;
- List<string> pointFields;
- List<string> flagNames;
- Thread readInThread = null;
- string url;
- int elapsedTime;
- readInWebpage readIn;
- string downloadedData;
- List<teamDataItem> tempData;
- string rtbData;
- //-------------------------------------------------------------------------------------------------------------------
- public Form1 ()
- {
- InitializeComponent();
- teamData = new List<teamDataItem>();
- pointFields = new List<string>() { };
- flagNames = new List<string>() { };
- url = "http://www.reddit.com/new/";
- elapsedTime = 15;
- readIn = new readInWebpage(url, "", elapsedTime, new Return_CallBack(updateForm));
- button2.Enabled = false;
- //setupDataGridView.setup(ref pointFields, ref flagNames, ref dataGridView1); //used to set up the columns
- downloadedData = "";
- tempData = new List<teamDataItem>();
- rtbData = "";
- this.readInThread = new Thread(readIn.runScan);
- this.updateForm_Thread = new Thread(() => parseData.strip(teamData, downloadedData, pointFields, flagNames, new Return_FormData(updateForm_Data))); //parse the data
- this.updateForm_Thread_num2 = new Thread(() => thread_UpdateFormSafe(tempData, rtbData)); //used to actually apply the updated data to the form
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void button1_Click (object sender, EventArgs e) //start
- {
- readIn.stopRun = false;
- checkWebsite();
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void button2_Click (object sender, EventArgs e) //stop
- {
- //readInThread.Abort();
- readIn.stopRun = true;
- button2.Enabled = false; //change over the state of the buttons to prevent double click issues
- button1.Enabled = true;
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void checkWebsite () //used to start the thread
- {
- //this.readInThread = new Thread(readIn.runScan);
- readInThread.Start();
- button2.Enabled = true; //change over the state of the buttons to prevent double click issues
- button1.Enabled = false;
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void updateForm (string downloadedData) //the delegate class to call back to
- {
- lock (thisLock) //lock (mutex style)
- {
- this.downloadedData = downloadedData;
- //this.updateForm_Thread = new Thread(() => parseData.strip(teamData, downloadedData, pointFields, flagNames, new Return_FormData(updateForm_Data))); //parse the data
- this.updateForm_Thread.Start();
- //!!! when the updateForm_Data() function is called, this needs to be disposed of
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void updateForm_Data (List<teamDataItem> tempData, string rtbData) //call back by the parseData
- {
- lock (lockFormUpdate) //lock off the form update to one class
- {
- this.tempData = tempData;
- this.rtbData = rtbData;
- //this.updateForm_Thread_num2 = new Thread(() => thread_UpdateFormSafe(tempData, rtbData)); //used to actually apply the updated data to the form
- this.updateForm_Thread_num2.Start();
- //!!! once the form has been updated this needs to be disposed of
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void thread_UpdateFormSafe (List<teamDataItem> tempData, string rtbData) //used to update the form data
- {
- //!!!!! I NEVER UPDATE THE COMBOBOX (needs to be done through new entry only)
- //if index == -1, and newEntry == true, then it's a new entry
- //if index != -1, and newEntry == false, then it's an old entry who's value changed
- //if index == -1, and newEntry == false, then it's an old entry that was unchanged
- for (int i = 0; i < tempData.Count; i++) //go through the list
- {
- if (tempData [i].newEntry == true) //if a new entry was added
- {
- //this.addToDataGridView(tempData [i]);
- }
- else
- {
- if (tempData [i].index != -1) //an existing entry was updates
- {
- //this.editDataGridView(tempData [i]);
- }
- }
- }
- //!!!! ADD - the richTextBox display
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void addToDataGridView (teamDataItem tempEntry) //updates dataGridView1 by adding a new item (in thread safe)
- {
- if (this.dataGridView1.InvokeRequired) //if the calling thread is different then the one that created dataGridView1
- {
- SetDGV_New_Callback dgvCall = new SetDGV_New_Callback(addToDataGridView);
- this.Invoke(dgvCall, new object [] { tempEntry });
- }
- else //if the same thread that created dataGridView1
- {
- dataGridView1.Rows.Add();
- dataGridView1.Rows [dataGridView1.RowCount - 1].HeaderCell.Value = tempEntry.teamName;
- for (int i = 0; i < pointFields.Count; i++) //adds the point fields
- {
- dataGridView1 [i, (dataGridView1.Rows.Count - 1)].Value = tempEntry.points [i];
- }
- for (int i = 0; i < flagNames.Count; i++) //adds the flag fields
- {
- dataGridView1 [(pointFields.Count + i), (dataGridView1.Rows.Count - 1)].Value = tempEntry.flags [i]; //remember it must pass by the pointFields first
- }
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- private void editDataGridView (teamDataItem tempEntry) //updates dataGridView1 by editing an existing line (in thread safe)
- {
- if (this.dataGridView1.InvokeRequired) //if the calling thread is different then the one that created dataGridView1
- {
- SetDGV_Edit_Callback dgvCall = new SetDGV_Edit_Callback(editDataGridView);
- this.Invoke(dgvCall, new object [] { tempEntry });
- }
- else //if the same thread that created dataGridView1
- {
- int indexFound = -1;
- for (int i = 0; i < dataGridView1.RowCount; i++) //looks for the index
- {
- if (tempEntry.teamName == dataGridView1.Rows [i].HeaderCell.Value.ToString()) //found the same team
- {
- indexFound = i;
- break;
- }
- }
- if (indexFound != -1) //we found an entry
- {
- for (int i = 0; i < pointFields.Count; i++) //adds the point fields
- {
- dataGridView1 [i, indexFound].Value = tempEntry.points [i];
- }
- for (int i = 0; i < flagNames.Count; i++) //adds the flag fields
- {
- dataGridView1 [(pointFields.Count + i), indexFound].Value = tempEntry.flags [i]; //remember it must pass by the pointFields first
- }
- }
- //!!! No else in case a fluke happens and we need to add a new row instead (implement later)
- }
- }
- //-------------------------------------------------------------------------------------------------------------------
- }
- //===================================================================================================================
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement