简体   繁体   English

.NET CF WinCE上的EventArgs线程安全吗?

[英]Are EventArgs thread safe on .NET CF WinCE

I'm trying to maintain some horrible VB to get concurrency. 我正在尝试维护一些可怕的VB并发。

I want to delegate some Maths code to a thread and have it raise an event with EventArgs which contains a structure with the information with which to update a form. 我想将一些Maths代码委托给一个线程,并让它引发一个带有EventArgs的事件,该事件包含一个具有用于更新表单的信息的结构。 In the UI event handler, I'm using BeginInvoke so as not to block the thread because the UI update takes quite a bit of time, but the thread needs to carry on with the next set of Maths. 在UI事件处理程序中,我使用BeginInvoke以避免阻塞线程,因为UI更新会花费很多时间,但是线程需要继续进行下一组数学运算。

The previous programmer implemented a timer to call the Maths code and there are a gnats swarm of global variables which represent the results; 以前的程序员实现了一个计时器来调用Maths代码,并且有大量代表结果的全局变量。 no way can I implement data locking, it's too big change. 我无法实现数据锁定,这是一个很大的变化。 The problem with the timer solution is that the Maths code can't run while the UI is being updated (at a slower rate). 计时器解决方案的问题在于,在更新UI(速度较慢)时,无法运行Maths代码。 I've also considered splitting up the UI code across many timer events but this is also quite irksome to achieve to get a balance across the cycles. 我还考虑过将UI代码拆分为多个计时器事件,但是要在整个周期之间保持平衡也很难实现。

Are the EventArgs thread safe ie if the UI starts to use the passed EventArgs and the thread generates another event, OR should the UI clone a copy before control is transferred to the UI thread ? EventArgs线程是否安全,即如果UI开始使用传递的EventArgs并且该线程生成了另一个事件,或者UI应该在控制权转移到UI线程之前克隆一个副本?

I've written some test code which looks like this. 我写了一些看起来像这样的测试代码。

Private Observed As UIDelegatePattern.Observed

Private Delegate Sub ProcessDelegate(ByVal sender As Object, ByVal e As UIDelegatePattern.Observed.ProcessEventArgs)

Private Sub Render(ByVal sender As Object, ByVal e As UIDelegatePattern.Observed.ProcessEventArgs)
    If Me.InvokeRequired Then
        Dim d As ProcessDelegate = New ProcessDelegate(AddressOf Render)

        ' invoke on the UI thread asynchronously
        Me.BeginInvoke(d, New Object() {sender, e})
    Else
        ' prevent event overflow by removing the handler before the long rendering activity
        RemoveHandler Observed.EventHandler, AddressOf Render

        ' simulate many controls updates
        For i As Integer = 0 To 1000
            Me.Label1.Text = e.Message
        Next
        Me.Update()
        Me.Refresh()
        Application.DoEvents()

        ' add the handler back in for next time
        AddHandler Observed.EventHandler, AddressOf Render
    End If
End Sub

You've got a lot of information, and possible some other questions implied, in that post. 您在那篇文章中获得了很多信息,并且可能暗示了其他一些问题。 But to be specific, as long as the class that is raising the event creates a new EventArgs on each notification then you are fine and do not need to copy the EventArgs in the event listener. 但具体来说,只要引发事件的类在每个通知上创建一个新的EventArgs,那么您就可以了,不需要在事件侦听器中复制EventArgs。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM