用户需求:定时抓取ERP系统条码已扫描未审核单据
解决办法:
由于采用Timer控件会消耗系统资源,故采用线程处理。
由于线程调用方法不支持控件处理模式,故采用委托方法实现。
bool bStop;
public DataTable dt =
new DataTable();
public ScanedNotChecked_Frm()
{
InitializeComponent();
}
private void ScanedNotChecked_Frm_Load(
object sender, EventArgs e)
{
//定义线程
Thread LogThread =
new Thread(
new ThreadStart(DoService));
//设置线程为后台线程,那样进程里就不会有未关闭的程序了
LogThread.IsBackground =
true;
bStop =
true;
if (bStop ==
true)
{
Control.PageControl.CheckForIllegalCrossThreadCalls =
false;
LogThread.Start();
//起线程
}
}
private string GetList()
{
SqlParameter[] parms =
new SqlParameter[
0];
DBCon db =
new DBCon(
"ZM");
gdScanedNotChecked.DataSource = SqlHelper.ExecuteDataset(db.StringConnection(), CommandType.StoredProcedure,
"DZ_ScanedNotChecked", parms).Tables[
0];
return "重载";
}
int i =
0;
private void DoService()
{
while (
true)
{
bStop =
false;
//别的线程里的调用哇~
i++;
lblMessage.Text =
"第" + i.ToString() +
"次" + returnList(returnMylist) +
"数据";
System.Threading.Thread.Sleep(
1000*
60*
5);
}
}
private delegate string returnStrDelegate();
//搞个最简单滴取值滴方法~
private string returnMylist()
{
return GetList();
}
//判断一下是不是该用Invoke滴~,,不是就直接返回~
private string returnList(returnStrDelegate myDelegate)
{
if (
this.InvokeRequired)
{
return (
string)
this.Invoke(myDelegate);
}
else
{
return myDelegate();
}
}