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