Results 1 to 5 of 5

Thread: Error A blocking operation was interrupted by a call to WSACancelBlockingCall

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2021
    Posts
    2

    Question Error A blocking operation was interrupted by a call to WSACancelBlockingCall

    Hi,

    I'm only a beginner on vb.net.
    I've created an application to control some devices over TCP. All works well, but when I push disconnect I get a popup with error "A blocking operation was interrupted by a call to WSACancelBlockingCall"

    Below the main code. Can someone point me in the right direction?

    Code:
    Imports System.Net.Sockets
    Imports System.Text
    Public Class Form1
        Dim pos1 As Integer
        Dim pos2 As Integer
        Dim length As Integer
        Dim receivedData As String = ""
        Dim tunerClient As TcpClient
    
        Dim stopAllThreads As Boolean
    
        Dim connected As Boolean = False
        Dim count = 0
        Public myProcess As Process = New Process()
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Control.CheckForIllegalCrossThreadCalls = False
            Timer1.Enabled = False
            t1Groupbox.Visible = False
            t2Groupbox.Visible = False
            t3Groupbox.Visible = False
            t4Groupbox.Visible = False
            LO_groupbox.Visible = False
        End Sub
        Private Sub tunerWrite(ByVal command As String)
            Try
                Dim ns As NetworkStream = tunerClient.GetStream()
                ns.Write(Encoding.ASCII.GetBytes(command), 0, command.Length)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
            If (connect_BTN.Text = "Connect") Then
                count = 0
                connect_BTN.Enabled = False
                Board1.Enabled = False
                Board2.Enabled = False
                Board3.Enabled = False
                connect_BTN.Text = "Connecting..."
                connecting_Timer.Enabled = True
                Try
                    If (Board1.Checked = True) Then
                        tunerClient = New TcpClient("192.168.1.6", 2323)
                    ElseIf (Board2.Checked = True) Then
                        tunerClient = New TcpClient("192.168.1.7", 2323)
                    ElseIf (Board3.Checked = True) Then
                        tunerClient = New TcpClient("192.168.1.8", 2323)
                    End If
                    Threading.ThreadPool.QueueUserWorkItem(AddressOf ReceiveMessages)
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
                tunerWrite("A")
            Else
                connect_BTN.Enabled = True
                Board1.Enabled = True
                Board2.Enabled = True
                Board3.Enabled = True
                connect_BTN.Text = "Disconnect"
                Timer1.Enabled = False
                connected = False
                stopAllThreads = True
                tunerClient.Close()
    
                t1Groupbox.Visible = False
                t2Groupbox.Visible = False
                t3Groupbox.Visible = False
                t4Groupbox.Visible = False
                LO_groupbox.Visible = False
                connect_BTN.Text = "Connect"
            End If
        End Sub
        Private Sub ReceiveMessages(state As Object)
            Try
                While True
                    If stopAllThreads = True Then
                        Exit Sub
                    End If
                    Dim ns As NetworkStream = tunerClient.GetStream()
                    Dim toReceive(100000) As Byte
                    ns.Read(toReceive, 0, toReceive.Length)
                    Dim txt As String = Encoding.ASCII.GetString(toReceive)
                    receivedData &= txt
                End While
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub frmProgramma_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If connected = True Then
                stopAllThreads = True
                tunerClient.Close()
                connected = False
            End If
        End Sub
        Private Sub connecting_Timer_Tick(sender As Object, e As EventArgs) Handles connecting_Timer.Tick
            connecting_Timer.Enabled = False
            count = count + 1
            If (count <= 20) Then
                connected = True
                connect_BTN.Enabled = True
                connect_BTN.Text = "Disconnect"
                Timer1.Enabled = True
                LO_groupbox.Visible = True
                tunerWrite("A")
                receivedData = ""
            Else
                stopAllThreads = True
                tunerClient.Close()
                MsgBox("Unit Not detected!")
                connect_BTN.Text = "Connect"
                connect_BTN.Enabled = True
            End If
        End Sub
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If (connected) Then
                Dim tmp As String
                tmp = Microsoft.VisualBasic.Left(receivedData, 3)
                If ((receivedData.Contains("<") And receivedData.Contains(">"))) Then
                    parseData()
                End If
            End If
        End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: Error A blocking operation was interrupted by a call to WSACancelBlockingCall

    Welcome to the forum.

    I tried examining your code, but I can't debug it. What you posted depends on so many external components you're going to have to post your project or at least more of.

    However:
    1. No line breaks between procedures. This makes the code harder to read.
    2. All variables should be initialized when declared. Even if only this means declaring them as new or setting them to Nothing.
    3. Personally I prefer Private/Public insted of Dim for class/module level declarations.
    4. It seems control properties are being set at load time. Perhaps this should be done at design time?
    5. There is no need for parenthesis around conditions or " = True" in If statements.
    6. Try to avoid the Microsoft.VisualBasic namespace. For example, use MessageBox.Show instead of MsgBox.
    7. "receivedData &= txt" - This looks like something where a StringBuilder is more appropriate.
    Declare "receivedData" as StringBuilder (System.Text.StringBuilder) and apppend text using the Append method.
    You will have to add the .ToString method when reading the actual text.
    8. "count = count + 1" - in this case all arithmetic operators (+ - / \ *) can be replaced with "+= -= /= \= *=". Resulting in: "count += 1"
    9.
    Code:
        Dim tmp As String
        tmp = Microsoft.VisualBasic.Left(receivedData, 3)
    Should be:

    Code:
        Dim tmp As String = receivedData.SubString(0, 3)
    10. Use the AndAlso/OrElse operators instead of And/Or in conditions.
    11. There's no need to write "System.Object" or "System.EventArgs" if you have imported this namespace at project or class level already. This goes for all namespaces.
    12. ByVal operators are usually unnecessary in event handlers.
    Last edited by Peter Swinkels; Jun 14th, 2021 at 04:40 AM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2021
    Posts
    2

    Re: Error A blocking operation was interrupted by a call to WSACancelBlockingCall

    Thanks for your reply. I'm totally new in the world of vb.net. I based my code on an existing tutorial and after hours turned it into the direction I wanted. Usually I'm working with C++.

    Is it possible that I send you the whole code by email for checking?

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: Error A blocking operation was interrupted by a call to WSACancelBlockingCall

    Alright, you can e-mail it and I will take a look. There is no guarantee I can find a solution though. Is there any reason you don't want to post your project here?

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,369

    Re: Error A blocking operation was interrupted by a call to WSACancelBlockingCall

    Okay, I had a look. I can't replicate your issue. Here are however some more coding tips:

    13. There is no need to store a message in a string before it can be displayed by a message box.
    14. Look up string interpolation: https://docs.microsoft.com/en-us/dot...olated-strings
    15. There are a load of empty procedures at the bottom of your class. Why?
    16. When reading a single character from a string you can use the Chars method instead of SubString.
    17. Single character literals can be explicitly defined as having the Char data type by adding the "c" sigil. I.E.: "A"c instead of just "A".

Posting Permissions

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



Click Here to Expand Forum to Full Width