C#中的多线程使用 -- Thread 类: 使用回调函数从一个线程中检索数据

下面为一个完整的实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace conThreadCallBack
{
class Program
{
static void Main(string[] args)
{
Program pro
= new Program();
ThreadWithState tws
= new ThreadWithState("你好吗?", pro.ResultCallBack);
Thread th
= new Thread(tws.ThreadProc);
th.Start();
Console.WriteLine(
"主线程: 开始执行, 等待新线程 ...");
th.Join();
//等待线程 th 执行完毕后, 再执行主线程中的后面的语句.
Console.WriteLine("主线程: 新线程执行完毕.");

Console.ReadKey();
}
// 回调方法

private void ResultCallBack(string message)
{
Console.WriteLine(
"主线程: 传回的消息: {0}", message);
}
}

public delegate void CallBackDelegate(string message);

public class ThreadWithState
{
private string Message;
// 回调委托
private CallBackDelegate callback;

// 构造函数
public ThreadWithState(string message, CallBackDelegate callbackDelegate)
{
this.Message = message;
this.callback = callbackDelegate;
}

// 线程方法
public void ThreadProc()
{
Console.WriteLine(
"新线程: 开始执行 ..");
Console.WriteLine(
"新线程: 传入的消息: {0}", Message);
if (callback != null) callback("谢谢, 我很好呀!"); // 通过委托, 将数据回传给回调函数
}

}

}

按照上面的例子, 在实际应用中一个比较复杂的示例如下:

public partial class FormMain : Form
{
...

// 获取轨迹数据(当天) : 使用线程处理, 有传入参数和 返回数据(委托回调)
private void btnGetTrack_Click(object sender, EventArgs e)
{
int userid = (int)txtTrackRecUserID.Tag;
DateTime startTime
= DateTime.Today;
DateTime endTime
= DateTime.Now;

CallBackTrackInfo callInfo
= new CallBackTrackInfo(userid, startTime, endTime, UpdateTrackInfoList);
Thread getTrackThread
= new Thread(callInfo.ThreadProc);
getTrackThread.Start();
//getTrackThread.Join(); // 等返回数据后

}

private void UpdateTrackInfoList(List<VehicleGPSInfo> trackInfo) // 回调委托获得数据
{
listViewTrackRec.Clear();
for (int i = 0; i < trackInfo.Count; i++)
{
VehicleGPSInfo recItem
= trackInfo[i];
ListViewItem item
= listViewTrackRec.Items.Add(recItem.GpsTime.ToString());
item.Name
= recItem.ID.ToString();
item.SubItems.Add(String.Format(
"{0}, {1}", recItem.Lng, recItem.Lat));
item.SubItems.Add(recItem.Speed.ToString());
item.SubItems.Add(recItem.Direction);
}
}

}

public delegate void TrackInfoCallBackDelegate(List<VehicleGPSInfo> trackInfo); //回调委托

public class CallBackTrackInfo // 带返回数据类
{
private int UserID;
private DateTime StartTime;
private DateTime EndTime;

List
<VehicleGPSInfo> TrackInfo= null;
// 回调委托
private TrackInfoCallBackDelegate callback;
// 构造函数
public CallBackTrackInfo(int userid, DateTime startTime, DateTime endTime, TrackInfoCallBackDelegate callbackDelegate)
{
this.UserID = userid;
this.StartTime = startTime;
this.EndTime = endTime;
this.callback = callbackDelegate;
}

// 线程方法: 连接数据库, 比较费时
public void ThreadProc()
{
DataTable dt
= DalClass.getDbMinuteMile(this.StartTime.ToString(), this.EndTime.ToString(), this.UserID);
int recCount = dt.Rows.Count;

if (recCount == 0) return;
if (recCount > 3500)
{
string message = String.Format("在选定时间段内的轨迹个数超出3500个,无法进行回放.\n"
+ "目前轨迹个数为:{0:d}个,请缩小回放轨迹的时间段,以减少回放轨迹个数.", recCount);
MessageBox.Show(message);
return;
}
TrackInfo
= new List<VehicleGPSInfo>();
DataRow row
= null;
for (int i = 0; i < recCount; i++)
{
row
= dt.Rows[i];
int uid = Convert.ToInt32(row["gpda_user_id"]);
DateTime gpsTime
= Convert.ToDateTime(row["gpda_datetime"]);
double lat = Convert.ToDouble(row["gpda_latitude"]);
double lng = Convert.ToDouble(row["gpda_longitude"]);
short speed = Convert.ToInt16(row["gpda_speed"]);
short angle = Convert.ToInt16(row["gpda_course"]);
short fuelValue = Convert.ToInt16(row["gpda_alarm"]);
uint status = Convert.ToUInt32(row["gpda_status"]);
VehicleGPSInfo recItem
= new VehicleGPSInfo(uid, gpsTime, lng, lat, speed, angle, fuelValue, status);
TrackInfo.Add(recItem);
}

if (callback != null) callback(TrackInfo); // 通过委托, 将数据回传给回调函数
}
}
posted @ 2011-06-21 14:58  沧海小小粟  阅读(12074)  评论(0编辑  收藏  举报