dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] problem An unhandled exception

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    15

    Resolved [RESOLVED] problem An unhandled exception

    when i'm receiving a message from a subscribed topic i get the following error in this line txtReceived.Text = ReceivedMessage

    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

    Additional information: Het is niet toegestaan een bewerking uit te voeren via verschillende threads: er werd vanaf een andere thread toegang gekregen tot het besturingselement txtReceived dan de thread waarop het element is gemaakt.
    How can i solve that in vb.net, i tryed some things but nothing helps excapt when i put it into a msgbox




    Code:
    Imports uPLibrary.Networking.M2Mqtt
    Imports uPLibrary.Networking.M2Mqtt.Messages
    
    
    Public Class Form1
    
        Private client As MqttClient
        Private clientId As String
        Private ReceivedMessage As String
    
        Public Sub New()
            InitializeComponent()
    
            Dim BrokerAddress As String = "192.**.**.**"
    
            client = New MqttClient(BrokerAddress)
            '  client.MqttMsgPublishReceived += AddressOf client_MqttMsgPublishReceived
    
            AddHandler client.MqttMsgPublishReceived, New EventHandler(AddressOf client_MqttMsgPublishReceived)
            clientId = Guid.NewGuid().ToString()
            client.Connect("1", "***", "***") 'clientId
        End Sub
    
        Protected Overrides Sub OnClosed(ByVal e As EventArgs)
            client.Disconnect()
            MyBase.OnClosed(e)
    
        End Sub
    
        Private Sub btnSubscribe_Click(sender As Object, e As EventArgs) Handles btnSubscribe.Click
            If txtTopicSubscribe.Text <> "" Then
                Dim Topic As String = txtTopicSubscribe.Text
                client.Subscribe(New String() {Topic}, New Byte() {2})
                'txtReceived.Text = ""
            Else
                MsgBox("You have to enter a topic to subscribe!")
            End If
        End Sub
    
        Sub client_MqttMsgPublishReceived(ByVal sender As Object, ByVal e As MqttMsgPublishEventArgs)
            ReceivedMessage = Encoding.UTF8.GetString(e.Message)
    
            'MsgBox(ReceivedMessage)
            txtReceived.Text = ReceivedMessage
    
        End Sub
    
    
        Private Sub btnPublish_Click(sender As Object, e As EventArgs) Handles btnPublish.Click
            If txtTopicPublish.Text <> "" Then
                Dim Topic As String = txtTopicPublish.Text
                client.Publish(Topic, Encoding.UTF8.GetBytes(txtPublish.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, True)
            Else
                MsgBox("You have to enter a topic to publish!")
            End If
        End Sub
    End Class

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,282

    Re: problem An unhandled exception

    Quote Originally Posted by jdsoft View Post
    Additional information: Het is niet toegestaan een bewerking uit te voeren via verschillende threads: er werd vanaf een andere thread toegang gekregen tot het besturingselement txtReceived dan de thread waarop het element is gemaakt.
    from Google Translate:
    It is not permitted to perform an operation through different threads: access to the txtReceived control from a different thread than the thread on which the element was created.

    So it looks like you are accessing WinForm Controls from a worker thread. You'll find lots of information on how to do this properly on this forum and many others.

    Assuming the MqttMsgPublishReceived event is always raised on a worker thread, then I'd probably do it as:
    Code:
    txtReceived.Invoke(Sub() txtReceived.Text = ReceivedMessage)
    (written free hand, so may not be exactly correct)

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,415

    Re: problem An unhandled exception

    Here is a translated version of that error message:
    It is not permitted to perform an operation through different threads: access to the txtReceived control from a different thread than the thread on which the element was created.
    This is a common issue with threading - you can only access controls from the main UI thread, not in other threads. It seems that client_MqttMsgPublishReceived is called by another thread (which is a good idea for network based things, so makes sense here).

    The way to deal with it is to .Invoke (run on the main thread) the code that updates the textbox, which in simple terms you can do like this:
    Code:
                txtReceived.Invoke(Sub() txtReceived.Text = ReceivedMessage)
    If you want to deal with multiple controls (or multiple properties) then it would be better to create a sub to invoke, you can see discussion on the topic here:
    http://www.vbforums.com/showthread.p...ghlight=invoke

    edit: beaten to it, and it seems I was slow to type!

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2015
    Posts
    15

    Resolved Re: problem An unhandled exception

    thanks a lot it's solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width