Data Checker
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
namespace DataChecker
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class DataChecker : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtCoffeePrice;
private System.Windows.Forms.TextBox txtTeaPrice;
private System.Windows.Forms.TextBox txtGoldPrice;
private System.Windows.Forms.Label lblCoffeePrice;
private System.Windows.Forms.Label lblTeaPrice;
private System.Windows.Forms.Label lblGoldPrice;
private int lastCoffeePrice = 0;
private int lastTeaPrice = 0;
private int lastGoldPrice = 0;
private int allUpdates = 0;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private System.Threading.Timer t1 = null;
private System.Threading.Timer t2 = null;
private System.Threading.Timer t3 = null;
public DataChecker()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
t1 = new System.Threading.Timer(new TimerCallback(Timer_Callback), 'C', 0, 500);
t2 = new System.Threading.Timer(new TimerCallback(Timer_Callback), 'T', 1, 500);
t3 = new System.Threading.Timer(new TimerCallback(Timer_Callback), 'G', 2, 500);
/*WaitOrTimerCallback wotc = new WaitOrTimerCallback(GetData);
AutoResetEvent are = new AutoResetEvent(false);
ThreadPool.RegisterWaitForSingleObject(are, new WaitOrTimerCallback(GetData), 'C', 500, false);*/
}
protected void Timer_Callback(object state)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(GetData), state);
}
int retVal;
private void GetData(object type)
{
char priceType = (char)type;
string sql = null;
sql = "SELECT Price FROM tblPrices WHERE Type='"+priceType.ToString()+"'";
SqlConnection cn = new SqlConnection("Server=localhost; Database=Prices; Integrated Security=SSPI");
cn.Open();
SqlCommand cmd = new SqlCommand(sql, cn);
lock(this)
{
retVal = (int)cmd.ExecuteScalar();
switch(priceType)
{
case 'C':
lastCoffeePrice = Convert.ToInt32(txtCoffeePrice.Text);
txtCoffeePrice.Text = retVal.ToString();
break;
case 'T':
lastTeaPrice = Convert.ToInt32(txtTeaPrice.Text);
txtTeaPrice.Text = retVal.ToString();
break;
case 'G':
lastGoldPrice = Convert.ToInt32(txtGoldPrice.Text);
txtGoldPrice.Text = retVal.ToString();
break;
}
}
allUpdates++;
cn.Close();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txtCoffeePrice = new System.Windows.Forms.TextBox();
this.txtTeaPrice = new System.Windows.Forms.TextBox();
this.txtGoldPrice = new System.Windows.Forms.TextBox();
this.lblCoffeePrice = new System.Windows.Forms.Label();
this.lblTeaPrice = new System.Windows.Forms.Label();
this.lblGoldPrice = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtCoffeePrice
//
this.txtCoffeePrice.Location = new System.Drawing.Point(72, 24);
this.txtCoffeePrice.Name = "txtCoffeePrice";
this.txtCoffeePrice.TabIndex = 0;
this.txtCoffeePrice.Text = "0";
//
// txtTeaPrice
//
this.txtTeaPrice.Location = new System.Drawing.Point(192, 24);
this.txtTeaPrice.Name = "txtTeaPrice";
this.txtTeaPrice.TabIndex = 1;
this.txtTeaPrice.Text = "0";
//
// txtGoldPrice
//
this.txtGoldPrice.Location = new System.Drawing.Point(312, 24);
this.txtGoldPrice.Name = "txtGoldPrice";
this.txtGoldPrice.TabIndex = 2;
this.txtGoldPrice.Text = "0";
//
// lblCoffeePrice
//
this.lblCoffeePrice.Location = new System.Drawing.Point(72, 0);
this.lblCoffeePrice.Name = "lblCoffeePrice";
this.lblCoffeePrice.TabIndex = 3;
this.lblCoffeePrice.Text = "Coffee Price";
//
// lblTeaPrice
//
this.lblTeaPrice.Location = new System.Drawing.Point(192, 0);
this.lblTeaPrice.Name = "lblTeaPrice";
this.lblTeaPrice.TabIndex = 4;
this.lblTeaPrice.Text = "Tea Price";
//
// lblGoldPrice
//
this.lblGoldPrice.Location = new System.Drawing.Point(320, 0);
this.lblGoldPrice.Name = "lblGoldPrice";
this.lblGoldPrice.TabIndex = 5;
this.lblGoldPrice.Text = "Gold Price";
//
// DataChecker
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(424, 54);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lblGoldPrice,
this.lblTeaPrice,
this.lblCoffeePrice,
this.txtGoldPrice,
this.txtTeaPrice,
this.txtCoffeePrice});
this.MaximizeBox = false;
this.Name = "DataChecker";
this.Text = "Data Checker";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new DataChecker());
}
}
}
Related examples in the same category