I'm trying to raise an event from a UWP class library background task, but I need to marshal it on the main UI thread in the UWP app I plan to use the class library with. I'm looking for a way to marshal it from the background task though. I've looked through a couple of articles including this one: Raise Events in .NET on the main UI thread but its not making sense to me. Can anyone help me with the below code?
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)
Thanks to jmcilhinney, the below code works!
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)