Click here to Skip to main content
16,020,701 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Pressing the search thread is executed. But during the run thread other buttons are locked.
please help me
----------MY CODE-----------------------
VB
Imports System.Threading
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
    Dim tic As Integer = 0
    Public Shared cnnoledb As New OleDbConnection
    Public Shared cmdinsert As New OleDbCommand
    Public Shared cmdoledb As New OleDbCommand
    ' Public Shared rdroledb As OleDbDataReader
    Public t1 As System.Threading.Thread
    Public t2 As System.Threading.Thread
    Public Shared _value = 0
    Public Shared Sub join()
        Try
            Dim strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory _
            & "\data.mdb;Jet OLEDB:Database Password=ehsan"
            cnnoledb.ConnectionString = strconnection
            cnnoledb.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Public Shared Sub close1()
        cnnoledb.Close()
        cnnoledb.Dispose()
    End Sub
    Public Sub search()
        If Me.InvokeRequired Then
            Me.Invoke(Me.Invoke(New MethodInvoker(AddressOf search)))
        Else
            Try
                cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(txtid.Text)
                cmdoledb.CommandType = CommandType.Text
                cmdoledb.Connection = cnnoledb
                Dim rdroledb As OleDbDataReader = cmdoledb.ExecuteReader
                While rdroledb.Read = True
                    txtnam.Text &= rdroledb.Item(0).ToString
                    Thread.Sleep(2000)
                    tictac()
                End While
                t1.Abort()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            cmdoledb.Dispose()
        End If
        
    End Sub
    Public Sub tictac()
        If Timer1.Enabled = False Then
            Timer1.Enabled = True
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        tic += 1
        If tic = 1 Then
            lbl1.Visible = True
            lbl2.Visible = False
            lbl3.Visible = False
        ElseIf tic = 2 Then
            lbl1.Visible = True
            lbl2.Visible = True
            lbl3.Visible = False
        Else
            lbl1.Visible = True
            lbl2.Visible = True
            lbl3.Visible = True
            tic = 0
        End If
        lbl1.Update()
        lbl2.Update()
        lbl3.Update()
    End Sub
    Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
        join()
        t1 = New System.Threading.Thread(New ThreadStart(AddressOf start))
        t1.Start()
        'close1()
    End Sub
    Public Sub start()
        search()
    End Sub
End Class
Posted
Updated 19-May-10 1:27am
v2
Comments
Christian Graus 19-May-10 16:24pm    
Reason for my vote of 1
Because it's obvious to me that you're completing this project by asking us to do one task at a time, and not writing anything yourself.

1 solution

You'v put the search code into a thread, so far so good but the first thing you do in this thread (search method) is escaping the thread and going back to the GUI thread where you then do all the searching.

The thread.sleep causes the entire GUI (since that's the thread where your code is running now) to freeze (this is normal behavior).

You'll have to change your code to something like this: (disclaimer NOT tested)
Public Sub search()
       Try
                cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(txtid.Text)
                cmdoledb.CommandType = CommandType.Text
                cmdoledb.Connection = cnnoledb
                Dim rdroledb As OleDbDataReader = cmdoledb.ExecuteReader
                While rdroledb.Read = True
                    updatetext(rdroledb.Item(0).ToString)
                    Thread.Sleep(2000)
                    tictac()
                End While
                t1.Abort()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            cmdoledb.Dispose()
       
    End Sub
private sub updatetext (text)
If Me.InvokeRequired Then
            Me.Invoke(Me.Invoke(New MethodInvoker(AddressOf updatetext ,text)))
        Else
txtnam.Text &= text
end if
end sub


This should help you get on your way, I'll see if I can improve on it when I have some more time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900