Question : thread vb.net

Hello,

I'm making a program in VB.net where I want to read a barcode with scanner to a textbox.  In my program I start a thread for constantly checking if my textbox is filled with 8 characters my program has to check in a text file if that code exists and let me see a msgbox with the contents of the textbox.  My program is constantly telling me that my textbox is empty also when I have scanned so this isn't true.
what am I doing wrong?  Here is the code:

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
Option Strict Off
Option Explicit On
Imports System.Runtime.InteropServices
Imports System.IO.Ports, System.Threading
Imports VB = Microsoft.VisualBasic
Imports System.Text
 
Module InpOut32_Declarations
    'Inp and Out declarations for port I/O using inpout32.dll.
    Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" (ByVal PortAddress As Short) As Short
    Public Declare Sub Out Lib "inpout32.dll" Alias "Out32" (ByVal PortAddress As Short, ByVal Value As Short)
End Module
 
Public Class vbPoort
 
    Public Sub receive()
        While True
            If txtBarcode.Text.Length = 8 Then
                MsgBox(Me.txtBarcode.Text.Length().ToString)
                'Te zoeken bestand opzoeken
                lpSectionName = "Namen"
                MsgBox(lpSectionName)
                lpKeyName = txtBarcode.Text
                MsgBox(lpKeyName)
                lpReturnedString = New String(Chr(0), 255)
                'MsgBox(lpReturnedString)
                nSize = lpReturnedString.Length
                'MsgBox(nSize)
                nSize = GetPrivateProfileString(lpSectionName, lpKeyName, "NO ", lpReturnedString, nSize, lpFilename)
                lpReturnedString = lpReturnedString.Substring(0, nSize)
                'MsgBox(lpReturnedString)
                SaveTextToFile(txtBarcode.Text & vbTab & DateTime.Today.ToString, My.Application.Info.DirectoryPath & "\Log.ini")
            End If
        End While
 
    End Sub
 
    Private Sub vbPoort_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        lblPoort.BackColor = Color.Lime
 
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        lblPoort.BackColor = Color.Lime
        'Bron definiëren waar het instellingen bestand staat.
        lpFilename = My.Application.Info.DirectoryPath & "\Instellingen.ini"
 
        Dim newThread As New Thread(AddressOf Me.receive)
        newThread.IsBackground = True
        newThread.Start()
 
    End Sub
End Class

Answer : thread vb.net

Is there a reason why you are not handling the text changed event of the text box?

It looks like that your doing isnt thread safe, your trying to access a property of the form when not in the forms thread. you do that by calling me.invoke eg..

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
    Private CheckTextDelegate As New ControlFormThreadDelegate(AddressOf Receive)
    Private Delegate Sub ControlFormThreadDelegate()
 
    Private Sub ThreadStartSub()
        While True
            Me.invoke(CheckTextDelegate)
 
            'thread.sleep pehaps? 
        End While
    End Sub
 
    Private Sub Receive()
 
        '..... all your check text code here
 
    End Sub
 
 
    Private newThread As New Thread(AddressOf Me.ThreadStartSub)
    Private Sub vbPoort_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        '...
        newThread.IsBackground = True
        newThread.Start()
    End Sub
Random Solutions  
 
programming4us programming4us