来C#版 交流个 vb.net DoEvents 很奇怪的问题

a3212b12 2012-12-21 11:42:36
今天 使用 vb.net DoEvents碰到一个很奇怪的问题,就是DoEvents后面的代码不执行。

程序是在一个函数内,启动了一个timer做一些工作,然后主函数等待,检查某个变量,知道这个变量为某值,才结束等待,并返回相应的值。被主函数检查的变量, 在timer事件中处理程序中赋值。

大概的例子可能是如下:故障现象是:主过程中 str1 = fun1()前面的语句能正常执行,后面的程序不会运行。调试监控后发现:程序 运行到 Application.DoEvents() 结束,不会再继续运行。

而且这一现象不是没回都出现,而是有时发生,有时不发生。

不知各位有没有碰到次问题。

--------示例代码:-----------------------

Sub main1()
。。。。。。
Dim str1 As String
str1 = fun1()
。。。。。。
End Sub



Function fun1() As String
t1 = New Timer()
t1.Interval = 2000
AddHandler t1.Tick, AddressOf t1_Tick
Dim ii As Int16
t1.Start()
For ii = 0 To 2 Step 0

If v_xx = 1 Then
Exit For
End If
Application.DoEvents()
Next
Return v_xx.ToString
End Function

Dim t1 As Timer
Dim v_xx As UInt16 = 0
Private Sub t1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Try
t1.Stop()
If read_data() = True Then
v_xx = 1
End If
Catch ex As Exception
Finally
If v_xx <> 1 Then
t1.Start()
End If
End Try
End Sub

Function read_data() As Boolean '读取数据,例如串口通讯程序
'具体代码
。。。。。。
End Function
...全文
100 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
a3212b12 2013-01-17
  • 打赏
  • 举报
回复
人气差啊!!!!!
a3212b12 2012-12-24
  • 打赏
  • 举报
回复
没人看到?自己顶下了!
可以设置定时关机,一段时间后关机 Private Sub Command1_Click() h = Combo1.Text s = Combo2.Text If (h >= 0 And h <= 23 And s >= 0 And s <= 59) Then Text1.Text = Combo1.Text + ":" + Combo2.Text Form2.Hide Else: MsgBox "你输入的数据格式不正确,是否继续?", vbOKOnly, "错误提示" End If Shell "cmd /c at text1.Text shutdown -s -t 5" End Sub Private Sub Command2_Click() Dim Savetime As Double Savetime = timeGetTime '记下开始时的时间 Form2.Hide While timeGetTime < Savetime + 3600000 '循环等待 DoEvents '转让控制权,以便让操作系统处理其它的事件 Wend Shell "cmd /c shutdown -s -t 5" Form2.Show End Sub Private Sub Command3_Click() Dim Savetime As Double Savetime = timeGetTime '记下开始时的时间 Form2.Hide While timeGetTime < Savetime + 7200000 '循环等待 DoEvents '转让控制权,以便让操作系统处理其它的事件 Wend Shell "cmd /c shutdown -s -t 5" Form2.Show End Sub Private Sub Command4_Click() Dim Savetime As Double Savetime = timeGetTime '记下开始时的时间 Form2.Hide While timeGetTime < Savetime + 18000000 '循环等待 DoEvents '转让控制权,以便让操作系统处理其它的事件 Wend Shell "cmd /c shutdown -s -t 5" Form2.Show End Sub Private Sub Command5_Click() s = InputBox("请换算成秒(1小时=3600秒,1分钟=60秒)", "多久后关机?") Dim Savetime As Double Savetime = timeGetTime '记下开始时的时间 Form2.Hide While timeGetTime < Savetime + s * 1000 '循环等待 DoEvents '转让控制权,以便让操作系统处理其它的事件 Wend Shell "cmd /c shutdown -s -t 5" Form2.Show End Sub Private Sub Command6_Click() End End Sub Private Sub Command7_Click() Shell "cmd /c shutdown -a" End Sub Private Sub Timer1_Timer() Label2.Caption = Format(Time, "h:mm") If (Text1.Text = Label2.Caption) Then Shell "cmd /c shutdown -s -t 5" Timer1.Enabled = False Form2.Show End If End Sub

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧