Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Changing a PictureBox Image on a SerialPort_DataReceived Event Yields Runtime Error: Invalid Operation Exception was unhandled
 

Changing a PictureBox Image on a SerialPort_DataReceived Event Yields Runtime Error: Invalid Operation Exception was unhandled

Any thoughts on what might cause a thread error like this?

Error:

InvalidOperationException was unhandled

Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.

Relavent Code:
Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Call ChangeImage()

End Sub



Sub ChangeImage()

        PictureBox1.Image = My.Resources._200_2

        PictureBox1.Image = My.Resources._200_1

End Sub

ngmvista  Saturday, November 28, 2009 1:27 AM
DataReceived is invoked on a thread other than the main UI thread, so it is necessary to use Invoke to perform the actual set of Image on the main UI thread.

Youprobably don't want to hold up receivingdata while updating the UI, nor do you want to update the UI too often when a lot of data is coming in. Therefore, I will show a technique that makes use of a timer.

    Private m_Timer As Timer
    Private m_ImageCounter As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_Timer = New Timer
        m_Timer.Interval = 250
        AddHandler m_Timer.Tick, AddressOf HandleTick
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If Not m_Timer Is Nothing Then
            m_Timer.Dispose()
        End If
    End Sub

    Private Sub SerialPort_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        Me.BeginInvoke(New MethodInvoker(AddressOf StartTimer))
    End Sub

    Private Sub StartTimer()
        If Not m_Timer.Enabled Then
            m_ImageCounter = 0
            m_Timer.Start()
        End If
    End Sub

    Private Sub HandleTick(ByVal sender As Object, ByVal e As EventArgs)
        If m_ImageCounter = 0 Then
            PictureBox1.Image = My.Resources._200_2
        ElseIf m_ImageCounter = 1 Then
            PictureBox1.Image = My.Resources._200_1
            m_Timer.Stop()
        End If
        m_ImageCounter += 1
    End Sub





BinaryCoder  Saturday, November 28, 2009 1:39 AM

You can use google to search for other answers

Custom Search

More Threads

• My Object not responding
• Back-In-The-Day
• HScrollBar and AutoRedraw
• can a loop statement use multiple elements?
• Traversing all subdirectories and all files in those subdirectories
• I can't use PrintPreview two times... (SOLVED)
• a loop problem
• Secure\Encrypted OLEDB Connection
• database to word
• How to fillup form from database?