二.下面是我在项目种的应用,主要涉及到委托、定时器、线程关闭和暂停以及MySql数据库的操作等示例结合,做起来感觉不错,线程也是应用到了,和大家分享!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;
using System.Threading;
using MySql.Data.MySqlClient;
namespace qxjshuju

...{
public partial class qxjshuju : Form

...{
delegate void SetTextCallback(string text);
//private Thread demoThread = null;
//private BackgroundWorker backgroundWorker1;

//用到的变量
ArrayList Zm_list;
ArrayList Zm_Name;
ArrayList starttime;
ArrayList endtime;
ArrayList windstation;
ArrayList windname;
ArrayList windstime;
ArrayList windetime;
public string strtext;
public bool sum;
public int con;
public bool st;
System.Timers.Timer t;
private MySqlConnection conn;
private SqlConnection innerconn;
private DataTable data;
private MySqlDataAdapter da;
private MySqlCommandBuilder cb;
public qxjshuju()

...{
InitializeComponent();
sum = true;
st = true;
con = 0;
this.lblxszt.Text = "服务运行中";
if (st)

...{
btnstart.Enabled = false;
}
t = new System.Timers.Timer(300000);//实例化Timer类,每5分种一次
t.Elapsed += new System.Timers.ElapsedEventHandler(ReadData);//到达时间的时候执行事件;
t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
ReadData(null, null);
}


线程数据提示语#region 线程数据提示语
private void ThreadProcUnsafe()

...{
this.txtbox.Text = "This text was set unsafely.";
}

private void ThreadProcSafe()

...{
this.SetText("This text was set safely.");
}
#endregion


利用委托方法把内容显示在textbox上#region 利用委托方法把内容显示在textbox上
private void SetText(string text)

...{
if (this.txtbox.InvokeRequired)

...{
SetTextCallback d = new SetTextCallback(SetText);

this.Invoke(d, new object[] ...{ text });
}
else

...{
this.txtbox.Text = text;
}
}
#endregion


函数入口#region 函数入口

/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

...{
Application.Run(new qxjshuju());
}
#endregion


读取气象局数据到宝山库中#region 读取气象局数据到宝山库中
public void ReadData(object source, System.Timers.ElapsedEventArgs e)

...{
try

...{
//------------------------------------------------Mondify By LiFuyun------------------------------------------------//
strtext = "";//记录显示在文本框里的文字
Zm_list = new ArrayList();
Zm_Name = new ArrayList();
starttime = new ArrayList();
endtime = new ArrayList();

//拿到站号和站名
string str = "select stationid,stationname from yl_zddz";
DataTable Zm_tb = GetSqlDT(str);
foreach (DataRow dr in Zm_tb.Rows)

...{
Zm_list.Add(dr[0]);
Zm_Name.Add(dr[1]);
}
//取得从哪个时间开始取数据(进行判断)
for (int i = 0; i < Zm_list.Count; i++)

...{
string str0 = "select top 1 station,date from ylz_count where station='" + Zm_list[i].ToString() + "' order by date desc";
DataTable dt = GetSqlDT(str0);
if (dt.Rows.Count > 0)

...{
foreach (DataRow dr in dt.Rows)

...{
starttime.Add(dr[1]);
}
}
else//从市里取时间

...{
string str1 = "select station,date from ylz_count where station='" + Zm_list[i].ToString() + "' order by date asc limit 1";
DataTable dt1 = GetMySqlDT(str1);
foreach (DataRow dr in dt1.Rows)

...{
starttime.Add(dr[1]);
}
}
}
//-------------------------------------------------------------------------------------------------------------------
//取得最后时间,此时间去市里库拿最新的,不过这个时间不能取得值,因为它不停地刷新着,我们要的是不会刷新的取下
for (int i = 0; i < Zm_list.Count; i++)

...{
string endsql = "select station,date from ylz_count where station='" + Zm_list[i].ToString() + "' order by date desc limit 1";
DataTable dt2 = GetMySqlDT(endsql);
foreach (DataRow dr in dt2.Rows)

...{
endtime.Add(dr[1]);
}
}
//-------------------------------------------------------------------------------------------------------------------
//屏幕显示站名、站号、开始时间等信息
strtext += "----------------------------站名、站号、开始时间---------------------------- ";
for (int i = 0; i < Zm_list.Count; i++)

...{
strtext += "站名:" + Zm_Name[i].ToString() + "[" + Zm_list[i].ToString() + "] 时间:" + starttime[i].ToString() + " ";
}
strtext += "---------------------------------------------------------------------------- ";
//-------------------------------------------------------------------------------------------------------------------
//查看读入雨量数据状态
string sql001, sql002, sql003, sql004;
int a = 1;
strtext += "----------------------------查看读入雨量数据状态---------------------------- ";
//strtext += "zmlist数:" + Zm_list.Count + " ";//测试代码
for (int i = 0; i < Zm_list.Count; i++)

...{
sql001 = "select date,station,yl_value from ylz_count where station='" + Zm_list[i].ToString() + "' and date>'" + Convert.ToDateTime(starttime[i].ToString()) + "' AND date<'" + Convert.ToDateTime(endtime[i].ToString()) + "' order by date desc";
DataTable dt3 = GetMySqlDT(sql001);
foreach (DataRow dr in dt3.Rows)

...{
sql002 = "select station,date,yl_value from ylz_count where station='" + Zm_list[i].ToString() + "' and date='" + dr[0].ToString() + "' order by date desc";
DataTable dt4 = GetSqlDT(sql002);
if (dt4.Rows.Count > 0)

...{
foreach (DataRow dr4 in dt4.Rows)

...{
sql003 = "update ylz_count set yl_value='" + dr4[2].ToString() + "' where station='" + Zm_list[i].ToString() + "' and date='" + dr[0].ToString() + "'";
DataSqlCom(sql003);
strtext += "第" + a + "条:站名[" + Zm_Name[i].ToString() + "] 雨量值已经修改宝山库中...... ";
a++;
}
}
else

...{
sql004 = "insert into ylz_count (date,station,yl_value)values('" + dr[0].ToString() + "','" + dr[1].ToString() + "','" + dr[2].ToString() + "')";
DataSqlCom(sql004);
strtext += "第" + a + "条:站名[" + Zm_Name[i].ToString() + "] 雨量值已经读入宝山库中...... ";
a++;
}
}
}
if (a == 1)

...{
strtext += "*****************************暂时无最新雨量值******************************* ";
}
strtext += "---------------------------------------------------------------------------- ";
//-------------------------------------------------------------------------------------------------------------------
//页面上显示最新的雨量数据
strtext += "各站读入最新时间雨量值如下: ";
string sql;
for (int i = 0; i < Zm_list.Count; i++)

...{
sql = "select date,station,yl_value from ylz_count where station='" + Zm_list[i].ToString() + "' order by date desc limit 1";
DataTable dt5 = GetMySqlDT(sql);
foreach (DataRow dr5 in dt5.Rows)

...{
strtext += "站名:" + Zm_Name[i].ToString() + "[" + Zm_list[i].ToString() + "] 雨量:" + dr5[2].ToString() + " 最新上传日期" + dr5[0].ToString() + " ";
}
}
strtext += "---------------------------------------------------------------------------- ";
//风速和风向开始-----------------------------------------------------------------------------------------------------
strtext += "----------------------以下是风速与风向数据情况一览表------------------------ ";
string wsql0, wsql1, wsql2, wsql3, wsql4, wsql5, wsql6, wsql7, wsql8;
int b = 1;
windstation = new ArrayList();
windname = new ArrayList();
windstime = new ArrayList();
windetime = new ArrayList();

//拿到站号和站名
wsql0 = "select stationid,stationname from zdz_zddz where type=1";
DataTable Zmtb = GetSqlDT(wsql0);
foreach (DataRow dr in Zmtb.Rows)

...{
windstation.Add(dr[0]);
windname.Add(dr[1]);
}
//取得从哪个时间开始取数据(进行判断)---------------------------------------------------------------------------------
for (int i = 0; i < windstation.Count; i++)

...{
wsql1 = "select top 1 date,station from zdz_instant where station='" + windstation[i].ToString() + "' order by date desc";
DataTable dt6 = GetSqlDT(wsql1);
if (dt6.Rows.Count > 0)

...{
foreach (DataRow dr6 in dt6.Rows)

...{
windstime.Add(dr6[0]);
}
}
else

...{
wsql2 = "select date,station from zdz_instant where station='" + windstation[i].ToString() + "' order by date asc limit 1";
DataTable dt7 = GetMySqlDT(wsql2);
if (dt7.Rows.Count > 0)

...{
foreach (DataRow dr7 in dt7.Rows)

...{
windstime.Add(dr7[0]);
}
}
}
}
//取得最后时间,此时间去市里库拿最新的,不过这个时间不能取得值,因为它不停地刷新着,我们要的是不会刷新的取下
for (int i = 0; i < windstation.Count; i++)

...{
wsql3 = "select date,station from zdz_instant where station='" + windstation[i].ToString() + "' order by date desc limit 1";
DataTable dt8 = GetMySqlDT(wsql3);
if (dt8.Rows.Count > 0)

...{
foreach (DataRow dr8 in dt8.Rows)

...{
windetime.Add(dr8[0]);
}
}
}
strtext += "风速与风向读入最新时间列表如下: ";
strtext += "---------------------------------------------------------------------------- ";
for (int i = 0; i < windstation.Count; i++)

...{
strtext += "站名:" + windname[i].ToString() + "[" + windstation[i].ToString() + "]" + " 开始时间:" + windstime[i].ToString() + " ";
}
strtext += "---------------------------------------------------------------------------- ";
strtext += "查看读入风速与风向数据状态如下: ";
strtext += "---------------------------------------------------------------------------- ";
for (int i = 0; i < windstation.Count; i++)

...{
wsql4 = "select date,station,instant_wd,instant_ws from zdz_instant where station='" + windstation[i].ToString() + "' and date>'" + Convert.ToDateTime(windstime[i].ToString()) + "' and date<'" + Convert.ToDateTime(windetime[i].ToString()) + "' order by date desc";
DataTable dt9 = GetMySqlDT(wsql4);
if (dt9.Rows.Count > 0)

...{
foreach (DataRow dr9 in dt9.Rows)

...{
wsql5 = "select date,station,instant_wd,instant_ws from zdz_instant where station='" + windstation[i].ToString() + "' and date='" + Convert.ToDateTime(dr9[0].ToString()) + "' order by date desc";
DataTable dt10 = GetSqlDT(wsql5);
if (dt10.Rows.Count > 0)

...{
foreach (DataRow dr10 in dt10.Rows)

...{
wsql6 = "update zdz_instant set instant_wd='" + dr10[2].ToString() + "',instant_ws='" + dr10[3].ToString() + "' where station='" + windstation[i].ToString() + "' and date='" + dr10[0].ToString() + "'";
DataSqlCom(wsql6);//如果宝山风速与风向库中有此站此时间的可以进行修改,而不是添加
strtext += "第" + b + "条-站名:" + windname[i].ToString() + "[" + windstation[i].ToString() + "] 风速与风向值:" + dr10[3].ToString() + "与" + dr10[2].ToString() + " 已经修改宝山库中...... ";
b++;
}
}
else

...{
wsql8 = "insert into zdz_instant (date,station,instant_wd,instant_ws)values('" + dr9[0].ToString() + "','" + dr9[1].ToString() + "','" + dr9[2].ToString() + "','" + dr9[3].ToString() + "')";
DataSqlCom(wsql8);//如果宝山风速与风向库中有此站此时间的可以进行修改,而不是添加
strtext += "第" + b + "条-站名:" + windname[i].ToString() + "[" + windstation[i].ToString() + "] 风速与风向值:" + dr9[3].ToString() + "与" + dr9[2].ToString() + " 已经读入宝山库中...... ";
b++;
}
}
}
}
if (b == 1)

...{
strtext += "***************************暂时无风速风向最新值***************************** ";
}
strtext += "---------------------------------------------------------------------------- ";
strtext += "各站读入最新时间风速与风向值如下: ";
strtext += "---------------------------------------------------------------------------- ";
for (int i = 0; i < windstation.Count; i++)

...{
wsql7 = "select date,station,instant_wd,instant_ws from zdz_instant where station='" + windstation[i].ToString() + "' order by date desc limit 1";
DataTable dt11 = GetMySqlDT(wsql7);
if (dt11.Rows.Count > 0)

...{
foreach (DataRow dr11 in dt11.Rows)

...{
strtext += "站名:" + windname[i].ToString() + "[" + windstation[i].ToString() + "] 风速与风向值:" + dr11[3].ToString() + "与" + dr11[2].ToString() + " -最新上传日期:" + dr11[0].ToString() + " ";
}
}
}
strtext += "---------------------------------------------------------------------------- ";
//-------------------------------------------------------------------------------------------------------------------
}
catch(Exception ex)

...{
strtext += "error:" + ex.ToString() + " ";
//MessageBox.Show("error:" + ex.ToString(), "提示信息");
ReadData(null, null);//重新调用事件,反复写入或修改数据到宝山库里
}
finally

...{
SetText(strtext);//最后在屏幕上显示信息
//System.Environment.Exit(System.Environment.ExitCode);//退出进程中的EXE
//System.Environment.Exit(0);//退出进程中的EXE
}
}
#endregion


Sql与MySql数据库方法汇总#region Sql与MySql数据库方法汇总
//取宝山数据库中某条件下的某条信息
public DataTable GetSqlDT(string sqlstr)

...{

if (innerconn != null)
innerconn.Close();
innerconn = sqlconn.CreateConn();
SqlDataAdapter da = new SqlDataAdapter(sqlstr, innerconn);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
dt.AcceptChanges();
return dt;
}

//取气象局数据库中某条件下的某条信息
public DataTable GetMySqlDT(string sqlstr)

...{
if (conn != null)
conn.Close();
conn = sqlconn.CreateConnQxj();
data = new DataTable();

da = new MySqlDataAdapter(sqlstr, conn);
cb = new MySqlCommandBuilder(da);

da.Fill(data);
return data;
}

//执行Sql语句,宝山区数据库链接
public void DataSqlCom(string sqlstr)

...{
SqlConnection conn0 = sqlconn.CreateConn();
SqlCommand sqlcom = new SqlCommand(sqlstr, conn0);
sqlcom.ExecuteNonQuery();
conn0.Close();
}

//执行MySql语句,气象局数据库链接
public void DataMySqlCom(string sqlstr)

...{
string sqlstring;
if (conn != null)
conn.Close();
sqlstring = ConfigurationSettings.AppSettings["qxjconstr"];//---------------------气象局MySql库链接字符串
conn = new MySqlConnection(sqlstring);
conn.Open();
MySqlCommand sqlcom = new MySqlCommand(sqlstr, conn);
sqlcom.ExecuteNonQuery();
conn.Close();
}
#endregion


点击右下角图标应用程序显示与隐藏#region 点击右下角图标应用程序显示与隐藏
private void notifyIcon1_Click(object sender, EventArgs e)

...{
if (con % 2 == 0)

...{
this.Visible = false;
}
else

...{
this.Visible = true;
}
con++;
}
#endregion


服务启动#region 服务启动
private void btnstart_Click(object sender, EventArgs e)

...{
btnstart.Enabled = false;
btnstop.Enabled = true;
//t.Enabled = true;
t.Start();
this.lblxszt.Text = "运行中";
this.lblxszt.ForeColor = System.Drawing.Color.Green;
}
#endregion


服务停止#region 服务停止
private void btnstop_Click(object sender, EventArgs e)

...{
btnstart.Enabled = true;
btnstop.Enabled = false;
//t.Enabled = false;
t.Stop();
this.lblxszt.Text = "已停止";
this.lblxszt.ForeColor = System.Drawing.Color.Red;
t.Close();
}
#endregion


服务退出#region 服务退出
private void btnout_Click(object sender, EventArgs e)

...{
t.Close();
Application.Exit();//退出应用程序
}
#endregion
}
}
当然,学习线程靠这两篇文章代码是不够的,需多加努力学习!望感兴趣的与我联系讨论!