C#多线程操作控件与定时器用法精辟示例(给合了数据库)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#多线程操作控件与定时器用法精辟示例【2】

作者:深海软件信息技术服务网【云中*深海】文章来源: 发布日期:2010-6-21 9:46:05 浏览次数:374 打印这篇文章

二.下面是我在项目种的应用,主要涉及到委托、定时器、线程关闭和暂停以及MySql数据库的操作等示例结合,做起来感觉不错,线程也是应用到了,和大家分享!

using System;

using System.Collections.Generic;

using ponentModel;

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 DataT able 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 函数入口

/**////

/// 应用程序的主入口点。

///

[STAThread]

static void Main()

{

Application.Run(new qxjshuju());

}

#endregion

读取气象局数据到宝山库中#region 读取气象局数据到宝山库中

public void ReadData(object source, System.Timers.ElapsedEventArgs e)

{

try

{

相关文档
最新文档