日期:2014-05-20  浏览次数:20911 次

求多线程例子
现用C#开发一大型程序,数据操作量较大

耗时太长,打算采用多线程提高效率

那为仁兄有多线程方面比较成熟和实用的代码

给一个,谢谢!

mail:xiaotian_net@163.com

------解决方案--------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
using System.Collections;

namespace AutoMessager
{
delegate void myDelegate();
delegate void SetTextCallback(string text);

public partial class frmAutoMsg : Form
{
event myDelegate myEvent;
string connStr = string.Empty;
Thread thd;
//private Icon eyeIcon;

//private NotifyIconEx notifyIconA;
//private NotifyIconEx notifyIconB;
private bool canClosed = false;

public frmAutoMsg()
{
this.ShowInTaskbar = false;
InitializeComponent();
//eyeIcon = new Icon(GetType(), "EYE.ICO ");
notifyIcon1.ContextMenu = contextMenuB;
}

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtMsgStatus.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.txtMsgStatus.Text += text;
}
}


private void frmAutoMsg_Load(object sender, EventArgs e)
{
connStr = System.Configuration.ConfigurationManager.AppSettings[ "ConnString "];
thd = new Thread(new ThreadStart(doEvent));
thd.IsBackground = true;
thd.Start();
//doEvent();
//notifyIcon1.Visible = true;
}

/// <summary>
/// 员工合同到期提醒
/// </summary>
void UpUserState()
{
try{
//数据库操作 省略... :)
SetText( "\r\n系统提示: 职员合同消息更新成功!\r\n ");
SetText( "执行时间: " + now.ToString( "yyyy-MM-dd HH:mm:ss ") + "\r\n ");


}
catch (Exception ex)
{
dr.Close();
tran.Rollback();
SetText( "\r\n系统错误: 职员合同消息更新错误: " + ex.Message + "\r\n ");
SetText( "执行时间: " + now.ToString( "yyyy-MM-dd HH:mm:ss ") + "\r\n ");
}
finally
{
dr.Close();
conn.Close();
conn.Dispose();
}
}
/// <summary>
/// 采购及供货 到货提醒
/// </summary>
void UpCaiGou()
{
try{
//数据库操作 省略... :)
SetText( "系统提示: 合同采购消息更新成功!\r\n ");
SetText( "执行时间: " + now.ToString( "yyyy-MM-dd HH:mm:ss ") + "\r\n ");


}
catch (Exception ex)