使用backgroundworker时是否可以更新doWork中的视图? [重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

我创建了一个考勤登录应用程序,允许学生通过在NFC阅读器上点击他们的卡来登录。讲师将能够跟踪谁已登录,因为在讲师的门户上显示了出勤屏幕,这将为尚未参加的学生显示红色圆圈,为已登录的学生显示绿色。

我使用背景工作者,这将允许NFC阅读器持续监听要扫描的卡,并且一旦它需要更新UI。这是我第一次与背景工作者合作,因此我对使用它们的知识不足。问题是,当扫描卡并识别学生时,我不知道如何更新UI。

 private void WaitChangeStatus(object sender, DoWorkEventArgs e)
        {
            nfcSer = new MainWindow();
            while (!e.Cancel)
            {

                if (nfcSer.connectCard())
                {
                    //this updates the student's attendance status to true
                    StudentsController.setAttendStudent(nfcSer.getcardUID(), Convert.ToInt32(Session["courseID"]));

                    //this calls the method which returns the same view of the attendance list of students however the student attendance status is changed.
                    classStudents(Session["ModuleName"].ToString());
                }
            }
        }
c# .net backgroundworker
1个回答
-1
投票

使用

SynchronizationContext.Current

在程序的开头(在ui-thread内)来存储它的synchronizationcontext。然后你可以使用

 SynchronizationContext.Post(SendOrPostCallback, Object)

在UI-Thread上运行代码。

© www.soinside.com 2019 - 2024. All rights reserved.