-1

please check the attached picture. i want to receive data from another application to Visual studio windows form through serial port and apply some conditions to the received data as shown in the picture, but unfortunately i got this error and i tried more to fix it but in vain. I'm asking your kind help to over ride this problem and edit my code. Thanks

Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived


        Dim value As Byte = SerialPort1.ReadLine()

        If (((value) & 2) <> 0) Then

            Label4.Visible = True
        End If
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Open()
    End Sub
FAB
  • 2,505
  • 1
  • 10
  • 21
  • I think you might be looking for the bitwise operator which is And in vb.net. – Mary Jun 03 '19 at 04:59
  • If you put 2 with anything it will be <> 0 anyway right? Can you please print that (value) & 2 so we see what you get? – OctaCode Jun 03 '19 at 05:22
  • Possible duplicate of [How can I run code in a background thread and still access the UI?](https://stackoverflow.com/a/45571728) – Visual Vincent Jun 03 '19 at 15:27

2 Answers2

1

I think you cannot access label4 directly when using Serial Port Reading. Try replacing Label4.Visible = True with:

if Me.InvokeRequired then
    Me.Invoke(Sub() Label4.Visible = True)
else
    Label4.Visible = True
end if
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
OctaCode
  • 635
  • 1
  • 4
  • 10
1

@Devcon: Don't use Invoke as it leads often to deadlocks, always use BeginInvoke instead (and I would use the methods and properties of the target control and not of the form, but that usually does not matter).

With Label4
    If .InvokeRequired Then
        .BeginInvoke(Sub() .Visible = True)
    Else
        .Visible = True
    End If
End With
Christoph
  • 3,322
  • 2
  • 19
  • 28
  • `Invoke` normally doesn't cause deadlocks unless you somehow misuse it, it works almost the exact same way as `BeginInvoke`. The only difference between them is that `Invoke` calls the specified method directly (and therefore blocks the calling thread until the method has finished executing), while `BeginInvoke` posts a message to the UI message loop telling it to do the same. If `Invoke` causes a deadlock then that would _likely_ happen for `BeginInvoke` as well, only this time causing the entire user interface to freeze instead. Which one to use depends on what you need (or don't need). – Visual Vincent Jun 03 '19 at 15:14
  • An example of a case where `Invoke` _can_ cause a deadlock is when you call it from both the UI thread and from another thread at the same time. However checking `InvokeRequired` like you do prevents this as it returns `False` on the UI thread. || As for which control to use: It works the exact same way no matter which control you choose, so like you said it doesn't matter. I, personally, prefer to use the form. :) - Ping @Devcon – Visual Vincent Jun 03 '19 at 15:15
  • 1
    Thanks @VisualVincent for clarifying, i never had problems with invoke nor using the form :) – OctaCode Jun 03 '19 at 15:19
  • @Visual Vincent: It doesn't need to be such a constellation, beleave me. I usually have only one non-UI thread and the UI thread (which does not use Invoke) but still experienced such behavior often. But feel free to use Invoke(..), me I never use it again, it costed me days to figure out what was going wrong and how to solve it! – Christoph Jun 04 '19 at 11:14
  • I never said it _must_ be under those circumstances either, but it was an example of a case when it happens. You use whichever method you prefer, of course. But like I said the two methods do pretty much the exact same thing, only at different times (I have inspected the source code and can verify this) so if you regularly experience deadlocks when using `Invoke` (in what should be normal use cases) there might be something else causing the trouble. Sometimes code that works on one computer (even the code of the framework) doesn't work the same way on another, without any apparent reason why. – Visual Vincent Jun 04 '19 at 11:38