线程问题 [英] Problems at thread
问题描述
Imports System.Threading
Public Class Form1
Public thread1 As New System.Threading.Thread(New ThreadStart(AddressOf connecttodata.search))
Dim tic As Integer = 0
Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
connecttodata.join()
connecttodata.insert()
End Sub
Private Sub btnsearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsearch.Click
connecttodata.join()
thread1.Start()
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
End Class
'Module connect
Module connect
Public Class connecttodata
Public Shared cnnoledb As New OleDbConnection
Public Shared cmdinsert As New OleDbCommand
Public Shared cmdoledb As New OleDbCommand
Public Shared rdroledb As OleDbDataReader
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 close()
cnnoledb.Close()
cnnoledb.Dispose()
End Sub
Public Shared Sub insert()
cmdinsert.CommandText = "INSERT INTO table1 (ID,NAME) VALUES(" & Form1.txtnumber.Text & ", '" & _
Form1.txtname.Text & " ')"
cmdinsert.CommandType = CommandType.Text
cmdinsert.Connection = cnnoledb
cmdinsert.ExecuteNonQuery()
MsgBox("inserted records")
cmdinsert.Dispose()
close()
End Sub
Public Shared Sub search()
Try
cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(Form1.txtid.Text)
cmdoledb.CommandType = CommandType.Text
cmdoledb.Connection = connecttodata.cnnoledb
rdroledb = cmdoledb.ExecuteReader
While rdroledb.Read = True
Form1.txtnam.Text &= rdroledb.Item(0).ToString
Thread.Sleep(200)
tictac()
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
cmdoledb.Dispose()
connecttodata.close()
Form1.Timer1.Enabled = False
Form1.thread1.Abort()
End Sub
Public Shared Sub tictac()
If Form1.Timer1.Enabled = False Then
Form1.Timer1.Enabled = True
End If
End Sub
End Class
End Module
-------------------------我的问题--------------------
``我不断收到以下错误消息:
从字符串"转换为类型"integer"是无效的."
''注意:如果我不使用线程,则错误将不存在.
-------------------------My question--------------------
''I keep getting this Following error:
''conversion from string "" to type "integer" is not valid.''
''note:If I do not use thread,An error does not have any.
推荐答案
嗨ehsan6000,
您不能从线程中调用用户界面,而应使用对UI的invoke来与主线程同步.只是谷歌"vb.net线程调用UI".
祝您好运!
Hi ehsan6000,
You CANNOT call the user interface from your thread and should use invoke to the UI to sync up with the main thread. Just google "vb.net Thread Invoke UI".
Good luck!
您已使用Invoke和Invoke之前需要进行以下操作: ^ ]
我们告诉您如何解决该问题.为什么要还原为旧方法而不使用它?
我唯一可以看到该错误的地方是:
You''ve used Invoke and Invoke required before :http://www.codeproject.com/Questions/82446/A-small-error-thread.aspx[^]
and we told you how to fix that problem. Why are you reverting back to old ways and not using it?
The only place that I can see where you would get that error is:
cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & CInt(Form1.txtid.Text)
如果Form1.txtid.Text不是有效的整数值,则将出现此错误.在这种情况下,Form1.txtid.Text看起来是空白.
您最好像这样使用TryParse:
If Form1.txtid.Text is not a valid integer value then you would get this error. It looks like in this case, that Form1.txtid.Text is blank.
You would be better using TryParse like this:
Dim intTemp as Integer
If Integer.TryParse(Form1.txtid.Text,intTemp)
cmdoledb.CommandText = "SELECT NAME FROM table1 WHERE ID=" & intTemp
Else
connecttodata.Close()
Return
End If
您也不需要中止线程.函数结束时,线程也将结束.没有必要告诉它终止作为最后一件事.
您也不需要测试布尔值是否为true或false(如
You also don''t need to abort the thread. When the function ends, so will the thread. Telling it to abort as the last thing is unnecessary.
You also don''t need to test boolean values against true or false (as in
While rdroledb.Read = True
如果它已经是一个布尔值,那么再次对其进行测试就是增加额外的处理.
If it already is a boolean value, then testing it again a boolean is just adding extra processing.
这篇关于线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!