【发布时间】:2019-01-19 16:27:53
【问题描述】:
我有一个 ACR122U NFC 阅读器,我可以在其中读取 MiFare UltraLight NFC 标签。我为我的读者下载了一个框架。但是当调用事件处理函数时,GUI的更新很慢。
我在线程编程和事件处理方面没有太多经验。也许我的问题的解决方案很容易。
但是,它仅在 UpdateInterface() 方法中“逐个标签”更新,并且一次更新一个。我可以看到每个标签都消失了。所以UI的更新很慢。
我猜这与后台读取器类调用的(硬件)事件有关。
程序一般都在运行,只是速度很慢。
private void StartMonitor()
{
//Function called when starting the Windows Forms Application
IMonitorFactory monitorfactory = MonitorFactory.Instance;
monitor = monitorfactory.Create(SCardScope.System);
monitor.Start(ReaderNames[0]);
monitor.StatusChanged += Monitor_StatusChanged;
}
private void Monitor_StatusChanged(object sender, StatusChangeEventArgs e)
{
strPassportNo = "";
strPassportNo = lPassportNumberNo.Text;
if (e.NewState.ToString().ToLower().Contains("empty".ToLower()) == true)
{
//Interface should be deleted all by once now as no NFC tag is on the reader.
UpdateInterface();
}
}
private void UpdateInterface()
{
if (InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}));
}
else
{
lPassportNumberNo.Text = "";
lPassengerName.Text = "";
lPax.Text = "";
lTableNo.Text = "";
lRoomNo.Text = "";
pbTables.Hide();
pbPasspic.Hide();
this.BackgroundImage = BackgroundWelcome;
}
}
预期的输出应该是 UI 的性能比实际快很多。或者至少其中包含数据的标签应该立即出现和消失。
【问题讨论】:
-
听起来您想批量处理来自 monitor.StatusChanged 事件的 NFC 更新。不过,我看不到您在哪里更新 UI。您需要使用 beginupdate 方法在 UI 线程上进行这些更新。