CheckForIllegalCrossThreadCalls
CheckForIllegalCrossThreadCalls允许子线呈随时更新UI,适用于子线程不断刷新数据源,UI线程不断显示数据源数据。使用情况举例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tdtk.MSMQ;
using System.Threading;
using System.Messaging;
namespace TDService
{
public partial class ThreadTest : Form
{
private DataTable dtblLog;
MqServerOperation MqOperLog = new MqServerOperation();
System.Timers.Timer tmTrick = new System.Timers.Timer();
public delegate void OperationDelegate();
public ThreadTest()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
tmTrick.Interval = 1000;
tmTrick.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeInvokeLog);
tmTrick.Enabled = true; tmTrick.Start();
if (InitServerQueue()) StartReciveThread();
}
/// <summary>
/// 初始化消息队列
/// </summary>
/// <returns></returns>
private bool InitServerQueue()
{
dtblLog = new DataTable(); dtblLog.Columns.Add("BangLogCode"); dtblLog.Columns.Add("CardCode"); dtblLog.Columns.Add("LogContent");
dtblLog.Columns.Add("PonderationType"); dtblLog.Columns.Add("LogTime"); dtblLog.Columns.Add("BangRoom"); dtblLog.Columns.Add("CupboardNo");
return MqOperLog.InitServerQueue("LogServer");
}
/// <summary>
/// 打开线程开始从消息队列读取
/// </summary>
private void StartReciveThread()
{
Thread thLogReciver = new Thread(new System.Threading.ThreadStart(LogReciverMessage));
thLogReciver.IsBackground = true; thLogReciver.SetApartmentState(System.Threading.ApartmentState.STA); thLogReciver.Start();
}
/// <summary>
/// 从LogServer消息队列中,实时接收日志数据(10毫秒)
/// </summary>
private void LogReciverMessage()
{
MessageQueue MqReciver = MqOperLog.GetServerMqReciver();
MqReciver.Formatter = new System.Messaging.BinaryMessageFormatter();
while (true)
{
System.Messaging.Message message = MqReciver.Receive();
try
{
string[] strLogArray = message.Body.ToString().Split(',');
if (strLogArray.Length == 7)
{
if (dtblLog.Rows.Count > 10)
{
dtblLog.Rows.RemoveAt(0);
}
DataRow dtrLog = dtblLog.NewRow();
dtrLog["BangLogCode"] = strLogArray[0];
dtrLog["CardCode"] = strLogArray[1];
dtrLog["LogContent"] = strLogArray[2];
dtrLog["PonderationType"] = strLogArray[3];
dtrLog["LogTime"] = strLogArray[4];
dtrLog["BangRoom"] = strLogArray[5];
dtrLog["CupboardNo"] = strLogArray[6];
dtblLog.Rows.Add(dtrLog); //在dtblLog中添加一行
}
}
catch (Exception exLog)
{
}
System.Threading.Thread.Sleep(10);
}
}
/// <summary>
/// 定时异步调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnTimeInvokeLog(object sender, EventArgs e)
{
OperationDelegate delgate = null;
delgate += new OperationDelegate(SetDataGrdView);
tmTrick.SynchronizingObject = this;
tmTrick.SynchronizingObject.BeginInvoke(delgate,null);
tmTrick.SynchronizingObject = null;
}
protected void SetDataGrdView()
{
dgvWeightBangLog.DataSource = dtblLog;
//dataGridView1.DataSource = dtblLog;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tdtk.MSMQ;
using System.Threading;
using System.Messaging;
namespace TDService
{
public partial class ThreadTest : Form
{
private DataTable dtblLog;
MqServerOperation MqOperLog = new MqServerOperation();
System.Timers.Timer tmTrick = new System.Timers.Timer();
public delegate void OperationDelegate();
public ThreadTest()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
tmTrick.Interval = 1000;
tmTrick.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeInvokeLog);
tmTrick.Enabled = true; tmTrick.Start();
if (InitServerQueue()) StartReciveThread();
}
/// <summary>
/// 初始化消息队列
/// </summary>
/// <returns></returns>
private bool InitServerQueue()
{
dtblLog = new DataTable(); dtblLog.Columns.Add("BangLogCode"); dtblLog.Columns.Add("CardCode"); dtblLog.Columns.Add("LogContent");
dtblLog.Columns.Add("PonderationType"); dtblLog.Columns.Add("LogTime"); dtblLog.Columns.Add("BangRoom"); dtblLog.Columns.Add("CupboardNo");
return MqOperLog.InitServerQueue("LogServer");
}
/// <summary>
/// 打开线程开始从消息队列读取
/// </summary>
private void StartReciveThread()
{
Thread thLogReciver = new Thread(new System.Threading.ThreadStart(LogReciverMessage));
thLogReciver.IsBackground = true; thLogReciver.SetApartmentState(System.Threading.ApartmentState.STA); thLogReciver.Start();
}
/// <summary>
/// 从LogServer消息队列中,实时接收日志数据(10毫秒)
/// </summary>
private void LogReciverMessage()
{
MessageQueue MqReciver = MqOperLog.GetServerMqReciver();
MqReciver.Formatter = new System.Messaging.BinaryMessageFormatter();
while (true)
{
System.Messaging.Message message = MqReciver.Receive();
try
{
string[] strLogArray = message.Body.ToString().Split(',');
if (strLogArray.Length == 7)
{
if (dtblLog.Rows.Count > 10)
{
dtblLog.Rows.RemoveAt(0);
}
DataRow dtrLog = dtblLog.NewRow();
dtrLog["BangLogCode"] = strLogArray[0];
dtrLog["CardCode"] = strLogArray[1];
dtrLog["LogContent"] = strLogArray[2];
dtrLog["PonderationType"] = strLogArray[3];
dtrLog["LogTime"] = strLogArray[4];
dtrLog["BangRoom"] = strLogArray[5];
dtrLog["CupboardNo"] = strLogArray[6];
dtblLog.Rows.Add(dtrLog); //在dtblLog中添加一行
}
}
catch (Exception exLog)
{
}
System.Threading.Thread.Sleep(10);
}
}
/// <summary>
/// 定时异步调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void OnTimeInvokeLog(object sender, EventArgs e)
{
OperationDelegate delgate = null;
delgate += new OperationDelegate(SetDataGrdView);
tmTrick.SynchronizingObject = this;
tmTrick.SynchronizingObject.BeginInvoke(delgate,null);
tmTrick.SynchronizingObject = null;
}
protected void SetDataGrdView()
{
dgvWeightBangLog.DataSource = dtblLog;
//dataGridView1.DataSource = dtblLog;
}
}
}