|
查看: 1981|回复: 22
|
为什么我的program不久后会hang的?
[复制链接]
|
|
|
你好,我想来想去还是解决不到,各位可以帮帮忙看看是哪里错了?这是visual basic 2010
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' ' ReadBtn.PerformClick()
Try
'Dim query As String = "SELECT * FROM lamps WHERE id =1"
Dim query As String = "SELECT * FROM lamps ORDER BY id DESC"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
Dim reader = cmd.ExecuteReader
If (reader.Read()) Then 'if data exist
lampvalue = reader.GetString(1) 'Get data from column 1 (lampstatus)
If (lampvalue <> temporary_1) Then
SerialPort1.Write(Chr(lampvalue))
temporary_1 = lampvalue
End If
reader.Close()
connection.Close()
End If
Catch ex As Exception
' Console.WriteLine(ex.Message)
End Try
Try
' Dim value As String
Dim temperature As Double
Dim datetime As Date
datetime = Now
Text_date.Text = datetime.ToShortDateString
Text_time.Text = datetime.ToLongTimeString
' Label1.Text = dataserial.ToString
' value = dataserial.ToString
' value = SerialPort1.ReadByte.ToString
dataserial = SerialPort1.ReadByte
temperature = (1.96) * dataserial
Label1.Text = temperature.ToString
Label5.Text = dataserial
If (dataserial <> temporary) Then
Dim query As String = "INSERT INTO lamps (date,time, Temperature_value) VALUES ('" & Text_date.Text & "','" & Text_time.Text & "','" & Label1.Text & "')"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
Dim rowsEffected As Integer = 0
connection.Open()
rowsEffected = cmd.ExecuteNonQuery
temporary = dataserial
connection.Close()
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
本帖最后由 wilson16 于 8-3-2013 05:55 PM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 9-3-2013 12:08 AM
|
显示全部楼层
楼主使用visual studio 2010吗?
有试着用debug mode来run这个program吗?结果怎样?
也许楼主可以试下放break point,看是在哪一个部分的时候才开始Hang的 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 9-3-2013 01:18 AM
|
显示全部楼层
zblue89 发表于 9-3-2013 12:08 AM 
楼主使用visual studio 2010吗?
有试着用debug mode来run这个program吗?结果怎样?
也许楼主可以试下放 ...
对,2010的。是不是check 每个program的step的?
我一个一个step按 ,下面一直出这个msg。。。
A first chance exception of type 'System.TimeoutException' occurred in System.dll
A first chance exception of type 'System.Data.SqlTypes.SqlNullValueException' occurred in MySql.Data.dll
A first chance exception of type 'System.TimeoutException' occurred in System.dll
A first chance exception of type 'System.Data.SqlTypes.SqlNullValueException' occurred in MySql.Data.dll
A first chance exception of type 'System.TimeoutException' occurred in System.dll
A first chance exception of type 'System.Data.SqlTypes.SqlNullValueException' occurred in MySql.Data.dll
A first chance exception of type 'System.TimeoutException' occurred in System.dll
过后是hang在这
Try
'Dim query As String = "SELECT * FROM lamps WHERE id =1"
Dim query As String = "SELECT * FROM lamps ORDER BY id DESC"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
connection.Open()
Dim reader = cmd.ExecuteReader
If (reader.Read()) Then 'if data exist
lampvalue = reader.GetString(1) 'Get data from column 1 (lampstatus)
If (lampvalue <> temporary_1) Then
SerialPort1.Write(Chr(lampvalue))
temporary_1 = lampvalue
因为我的program是这样的,一个是read from database 和 write to database。
如果我其中一个read 或 write 就ok。两个一起来就好像我现在这样会hang。
本帖最后由 wilson16 于 9-3-2013 01:54 AM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 9-3-2013 03:00 PM
|
显示全部楼层
Dead Lock?
可能帮不到你,
但一般规则 connection.Close() 是放在 Finally 的,
Try
'Do something
Catch ex As Exception
'Handle exception
Finally
connection.Close()
End Try
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 10-3-2013 12:54 PM
|
显示全部楼层
hkloke2000 发表于 9-3-2013 03:00 PM 
Dead Lock?
可能帮不到你,
请问你的statment要怎样放呢? 我是新手。
|
|
|
|
|
|
|
|
|
|
|
发表于 11-3-2013 10:23 AM
|
显示全部楼层
我很好奇为什么你要在timer里开启connection? connection 开启是需要时间的,如果timer的速度快过connection 开启的时间,是有可能会hang的。而且,我也看到你有一些variable有重叠到,尽量减少variable重叠。 本帖最后由 doktorkoo 于 11-3-2013 10:28 AM 编辑
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-3-2013 04:39 PM
|
显示全部楼层
doktorkoo 发表于 11-3-2013 10:23 AM 
我很好奇为什么你要在timer里开启connection? connection 开启是需要时间的,如果timer的速度快过connecti ...
是这样的,当我send data 去database 时,一定要经过vb。放在timer里时要给它自动更新data在database。我换了不一样名字的viariable还是一样出了以下msg。
A first chance exception of type 'System.TimeoutException' occurred in System.dll
A first chance exception of type 'System.Data.SqlTypes.SqlNullValueException' occurred in MySql.Data.dll
因为我有用series port,我加了readtimeout 和writetimeout的时间还是一样。
希望你可以帮帮忙。谢谢。
|
|
|
|
|
|
|
|
|
|
|
发表于 11-3-2013 08:45 PM
|
显示全部楼层
我觉得是开太多db connections了... mysql 处理不急。
去mysql console 用 “SHOW PROCESSLIST” 看看。 |
|
|
|
|
|
|
|
|
|
|
发表于 11-3-2013 09:01 PM
|
显示全部楼层
wilson16 发表于 11-3-2013 04:39 PM 
是这样的,当我send data 去database 时,一定要经过vb。放在timer里时要给它自动更新data在database。我 ...
想知道下你的timer 的interval是多少?可能你的interval过快,快过connection开启时间,而导致hang。
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-3-2013 09:20 PM
|
显示全部楼层
doktorkoo 发表于 11-3-2013 09:01 PM 
想知道下你的timer 的interval是多少?可能你的interval过快,快过connection开启时间,而导致hang。
目前我放 Readtimeout :100
receivebyts :100
writetimeout: 100
interval :1000
|
|
|
|
|
|
|
|
|
|
|
发表于 11-3-2013 10:37 PM
|
显示全部楼层
其实为什么你要一直开关db connections? 一条连接不够吗?而且的I/O在处理什么?db queries没必要这么频密吧?
注:别把DB当socket用  |
|
|
|
|
|
|
|
|
|
|
发表于 12-3-2013 04:47 AM
|
显示全部楼层
你为什么要 Comments这行?
' Console.WriteLine(ex.Message)
如果遇到错误,不会到这里(没有 Close connection)
reader.Close()
connection.Close()
当Timer Event 在执行,又有Error又没有 Close connection,
一段时间后,就hang咯.
我觉的因该用 synchronized method.
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-3-2013 02:03 PM
|
显示全部楼层
hkloke2000 发表于 12-3-2013 04:47 AM 
你为什么要 Comments这行?
' Console.WriteLine(ex.Message)
Dim temperature As Double
Dim datetime As Date
datetime = Now
Text_date.Text = datetime.ToShortDateString
Text_time.Text = datetime.ToLongTimeString
Try
dataserial = SerialPort1.ReadByte
temperature = (1.96) * dataserial
Label1.Text = temperature.ToString
Label5.Text = dataserial
If (dataserial <> temporary) Then
Dim query As String = "INSERT INTO lamps (date,time, Temperature_value) VALUES ('" & Text_date.Text & "','" & Text_time.Text & "','" & Label1.Text & "')"
Dim connection As New MySqlConnection(connStr)
Dim cmd As New MySqlCommand(query, connection)
Dim rowsEffected As Integer = 0
connection.Open()
rowsEffected = cmd.ExecuteNonQuery
temporary = dataserial
connection.Close()
End If
Catch ex As Exception
'Console.WriteLine(ex.Message)
End Try
其实我试过了是这部分的问题来的。意识就是还是有这msg
A first chance exception of type 'System.TimeoutException' occurred in System.dll。
在timer 里有两个部分TRY-End TRY。我试过只是放一个部分TRY-end try在timer 里,只有以上这部分会hang。
Console.WriteLine(ex.Message)我没用,变成comment了。
connection.Close()我也有放嘛,怎么还会出timeout的。
ok。其实我这部分是当我有一个hardware send 8bit的data(0-255)用serialport。它是不会send 同样data的。
然后去formula 后就display在text 和在send 去 database 里。时间日期都一样send去database里。用timer 是因为会更新拿到最新的data。
本帖最后由 wilson16 于 12-3-2013 02:19 PM 编辑
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-3-2013 02:23 PM
|
显示全部楼层
megablue 发表于 11-3-2013 10:37 PM 
其实为什么你要一直开关db connections? 一条连接不够吗?而且的I/O在处理什么?db queries没必要这么频密吧 ...
erm。。。我已经在#13楼解释了。麻烦你去看看吧。谢谢。
|
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-3-2013 02:25 PM
|
显示全部楼层
megablue 发表于 11-3-2013 08:45 PM 
我觉得是开太多db connections了... mysql 处理不急。
去mysql console 用 “SHOW PROCESSLIST” 看看。
mysql console在那里?我第一次 用而已
|
|
|
|
|
|
|
|
|
|
|
发表于 12-3-2013 09:48 PM
|
显示全部楼层
- Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
- Timer1.Enable=FALSE
- '把你的程式,放在这里!
- Timer1.Enable=TRUE
- End Sub
复制代码 试看这个方法有效否! |
|
|
|
|
|
|
|
|
|
|
发表于 12-3-2013 10:21 PM
|
显示全部楼层
|
这程式有问题。如果每秒要INsert的话,不必把connection又开又关。就让他开着,直到connection用完为止。(如果这个程式可以霸占一个connection没问题的话。) |
|
|
|
|
|
|
|
|
|
|
发表于 12-3-2013 10:22 PM
|
显示全部楼层
@啊@我看错了。你program serial port 不能用timer. 因为可能读不及。
serialport 要用callback event.
private void port_DataReceived(object sender,
SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer Console.WriteLine(port.ReadExisting()); }
看这个范例。
http://msmvps.com/blogs/coad/arc ... n-C_2300_-.NET.aspx
不过,话说回来,如果可以霸占一个connection就让它开着。因为你一直要INsert data,所以我认为是OKAY的。
本帖最后由 ch_leong 于 12-3-2013 10:33 PM 编辑
|
|
|
|
|
|
|
|
|
|
|
发表于 14-3-2013 01:52 AM
|
显示全部楼层
wilson16 发表于 12-3-2013 02:03 PM 
Dim temperature As Double
Dim datetime As Date
datetime = Now connection.Close()我也有放嘛,怎么还会出timeout的。
放错地方.
http://scn.sap.com/thread/3296432- ' Visual Basic
- Public Sub ConnectToOdbc()
- Timer1.Enable=False
- Dim conn As New System.Data.Odbc.OdbcConnection
- ' TODO: Modify the connection string and include any
- ' additional required properties for your database.
- conn.ConnectionString = & _
- "FIL=MS Access;DSN=valid data source name"
- Try
- conn.Open()
- '如果这里有错误
- '不会到这里1的
- '会飞到这里2
- '在飞到这里3
- '这里1
- Catch ex As Exception
- '这里2
- MessageBox.Show("Failed to connect to data source")
- Finally
- '这里3
- conn.Close()
- Timer1.Enable=TRUE
- End Try
- End Sub
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 15-3-2013 12:21 AM
|
显示全部楼层
hkloke2000 发表于 14-3-2013 01:52 AM 
放错地方.
http://scn.sap.com/thread/3296432
好的,我试试看。谢谢你。
|
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|