如何从UWP APP(vb.net)中的后台任务在主UI线程上引发事件

时间:2017-01-29 20:10:54

标签: vb.net uwp task

我试图从UWP类库后台任务中引发事件,但我需要在UWP应用程序中的主UI线程上封送它,我计划使用类库。我正在寻找一种方法来从后台任务中编组它。我查看过几篇文章,包括这篇文章: Raise Events in .NET on the main UI thread 但它对我没有意义。任何人都可以帮我解决下面的代码吗?

    Private ReceiveTask As New Task(Sub()    
                                    Dim InStream As Stream = CSocket.InputStream.AsStreamForRead    
                                    While killswitch = False    
                                        Try    
                                            Dim Reader As StreamReader = New StreamReader(InStream)    
                                            Dim DataText As String = ""    
                                            While Reader.Peek <> -1    
                                                DataText &= Convert.ToChar(Reader.Read)    
                                            End While    
                                            RaiseEvent DataReceived(DataText)    
                                        Catch ex As Exception    
                                            RaiseEvent SocketError("Receiving", ex.Message)    
                                        End Try    
                                    End While    
                                End Sub)    

感谢jmcilhinney,以下代码有效!

    Private ReceiveTask As New Task(Sub()
                                    Dim InStream As Stream = CSocket.InputStream.AsStreamForRead
                                    While killswitch = False
                                        Try
                                            Dim Reader As StreamReader = New StreamReader(InStream)
                                            If Reader.Peek <> -1 Then
                                                DataText = ""
                                                While Reader.Peek <> -1
                                                    DataText &= Convert.ToChar(Reader.Read)
                                                End While
                                                uiContext.Post(AddressOf RaiseDataToUI, Nothing)
                                            End If
                                        Catch ex As Exception
                                            ReceiveError = ex.Message
                                            uiContext.Post(AddressOf RaiseErrorToUI, Nothing)
                                        End Try
                                    End While
                                End Sub)

1 个答案:

答案 0 :(得分:0)

我自己没有做任何UWP开发,但我相信正确的方法是使用SynchronizationContext类,就像在WPF中一样。原则是SynchronizationContext.Current属性将产生类的线程特定实例,因此您可以在UI线程上获取该属性的值,然后在其他地方使用该对象来编组对拥有线程的调用。通常在UI线程上创建的对象的构造函数中检索属性值,例如

Imports System.Threading

Public Class SomeClass

    'Get the context for the thread on which the current instance is created.
    Private uiContext As SynchronizationContext = SynchronizationContext.Current

    Private Sub ThisMethodIsCalledOnASecondaryThread()
        uiContext.Post(AddressOf ThisMethodIsCalledOnTheUIThread, Nothing)
    End Sub

    Private Sub ThisMethodIsCalledOnTheUIThread(data As Object)
        'Execute UI thread logic here, e.g. raise an event.
    End Sub

End Class