13,190
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 标签读取事件
/// </summary>
/// <param name="list"></param>
void reader_TagsReport(List<TagInfo> list)
{
if (list.Count == 0) return;
bool tagNew = false;
DateTime? newReadTime = null;
list.ForEach((x) =>
{
var tag = listTag.Find(c => c.Epc == x.Epc);
if (tag == null)
{
var tagnew = new TagInfo()
{
Epc = x.Epc,
ReadCount = 1,
ReadTime = DateTime.Now,
ReaderId = x.ReaderId,
AntennaId = x.AntennaId
};
lock (listTag)
{
listTag.Add(tagnew);
newReadTime = DateTime.Now;
tagNew = true;
}
}
else
{
lock (listTag)
{
tag.ReadCount++;
tag.ReadTime = DateTime.Now;
tag.ReaderId = x.ReaderId;
tag.AntennaId = x.AntennaId;
}
}
});
//刷新UI
if (tagNew) //读取到新标签
{
this.BeginInvoke(new Action<bool, DateTime?>(BindData), true, newReadTime);
}
else //旧标签的重复读取
{
//进行时间间隔控制:如果刷新频率太高,即使跨线程调用也会把UI线程卡死的
DateTime dtNow = DateTime.Now;
if (lastRead == null || (dtNow - lastRead).TotalMilliseconds > 100)
{
this.BeginInvoke(new Action<bool, DateTime?>(BindData), false, null);
lastRead = dtNow;
}
}
}
/// <summary>
/// 绑定gridItems,设置格式
/// </summary>
/// <param name="readTime"></param>
/// <param name="isNew"></param>
private void BindData(bool isNew, DateTime? readTime)
{
var bindingList = this.gridEpcs.DataSource as BindingList<TagInfo>;
bindingList.ResetBindings();
if (isNew) lbCount.Text = listTag.Count.ToString();
if (readTime.HasValue)
{
TimeSpan ts = readTime.Value - readStart;
lbTimeCount.Text = ts.Seconds.ToString() + "s" + ts.Milliseconds.ToString();
}
}