如何快速运行 Device.BeginInvokeOnMainThread?

huangapple go评论57阅读模式
英文:

How to quickly run Device.BeginInvokeOnMainThread?

问题

通过TCP通信连续传入17个数据。我尝试将数据添加到与DataGrid绑定的模型中,但当我添加数据时,它会绑定到UI,因此我使用Device.BeginInvokeOnMainThread来添加它。然而,似乎UI线程在接收到所有17个数据后才开始工作,因为数据在线程池中积累起来,无法顺序添加。如何顺序添加数据?

await Task.Run(() =>
{
    Thread.CurrentThread.Priority = ThreadPriority.Highest;
    QuantitationMeasureData data = new QuantitationMeasureData()
    {
        GroupNumber = m_nCurRow,
        GroupName = "Group" + m_nCurRow,
        SampleName = "Sample" + m_nCurRow,
        Cell = e.Cell.ToString(),
        Time = GlobalParam.Time.Now,
        Wave = this.m_Model.STCInfo.Wavelength,
        Base1 = this.m_Model.STCInfo.BaseWave1,
        Base2 = this.m_Model.STCInfo.BaseWave2,
        Trans = e.Trans,
        ABS = dRetABS,
        Conc = dRetConc,
        Factor = this.m_Model.Factor,
        AVG = dRetABS,
        STD = dRetConc,
    };

    Device.BeginInvokeOnMainThread(() =>
    {
        this.m_Model.MeasureDataCollection.Add(data);
        this.xDataGrid.RefreshData();
    });
});

我认为没有正确的时机运行UI线程,因为从1到17的数据传入速度很快。

英文:

17 data come in consecutively through TCP communication. I'm trying to add data to the model bound to the datagrid, but when I add it, it's bound to the UI, so I'm using Device.BeginInvokeOnMainThread to add it. However, it seems that the ui thread works after receiving all 17 data because it is piled up in the thread pool and cannot be added sequentially. How do I add data sequentially?

await Task.Run(() =>
                    {
                        Thread.CurrentThread.Priority = ThreadPriority.Highest;
                        QuantitationMeasureData data = new QuantitationMeasureData()
                        {
                            GroupNumber = m_nCurRow,
                            GroupName = "Group" + m_nCurRow,
                            SampleName = "Sample" + m_nCurRow,
                            Cell = e.Cell.ToString(),
                            Time = GlobalParam.Time.Now,
                            Wave = this.m_Model.STCInfo.Wavelength,
                            Base1 = this.m_Model.STCInfo.BaseWave1,
                            Base2 = this.m_Model.STCInfo.BaseWave2,
                            Trans = e.Trans,
                            ABS = dRetABS,
                            Conc = dRetConc,
                            Factor = this.m_Model.Factor,
                            AVG = dRetABS,
                            STD = dRetConc,
                        };

                        Device.BeginInvokeOnMainThread(() =>
                        {
                            this.m_Model.MeasureDataCollection.Add(data);
                            this.xDataGrid.RefreshData();
                        });
                    });

I think there is no timing to run the ui thread because the data from 1 to 17 comes in quickly.

答案1

得分: 1

有一个用于UI工作的优先级队列。像Invoke这样的代码更新比重绘具有更高的优先级,因此只要有代码更新要执行,UI就不会自动重新绘制。这就是为什么您会看到多个更新同时发生。

如果您想逐个查看更新,您需要延迟它们。100毫秒是一个“非常快但足够慢以便被看到”的经验法则。您可以延迟将它们添加到UI中,或者使用UI动画。

英文:

There's a priority queue for UI work. Code updates such as Invoke have higher priority than redraws, so as long as there's a code update to do, the UI won't redraw itself. This is why you're seeing multiple updates simultaneously.

If you want to see the updates one at a time, you'll need to delay them. 100ms is a good rule of thumb for "really fast but slow enough to be seen". You can either delay adding them to the UI or use a UI animation.

huangapple
  • 本文由 发表于 2023年6月2日 07:26:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定