如何解决程序运行时cpu占用率100%

diablobaal 2005-11-04 02:17:39
我做了一个程序,我把5500个字节的数据放到一个数组里面,然后用一个for。。。next的循环,循环写500条记录,每条记录11个字节,在写记录时cpu占用率100%,如何解决??
...全文
355 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bilujun 2005-11-04
  • 打赏
  • 举报
回复
同意楼上,doevents只是转让控制权,以便让操作系统处理其它的事件。而sleep才是挂起当前线程的操作。
韧恒 2005-11-04
  • 打赏
  • 举报
回复
一个DoEvents是解决不了问题的。

简单点的方案使用Sleep,如果在循环过程中不希望用户进行干预,把DoEvents换成Sleep就行了,如果希望响应用户的输入,则在DoEvents前加入Sleep即可。
你试下下面的代码,在使用Sleep和不使用Sleep两种情况中试下便知。

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
Dim i As Long

For i = 0 To 90000000
Sleep 10
DoEvents
Next
End Sub
diablobaal 2005-11-04
  • 打赏
  • 举报
回复
试过了,不行
faysky2 2005-11-04
  • 打赏
  • 举报
回复
说错,不行你在代码中间再多加一个DoEvents,就在 '余额百元 下一行加上再试
faysky2 2005-11-04
  • 打赏
  • 举报
回复
你把DoEvents放最后肯定没有效果,那样跟不放完全一样,要放到For 下面的第一行
bbhere 2005-11-04
  • 打赏
  • 举报
回复
把 DoEvents移到For i = 1 To ie - 1下面试试
diablobaal 2005-11-04
  • 打赏
  • 举报
回复
ie=501
diablobaal 2005-11-04
  • 打赏
  • 举报
回复
代码如下:

Dim Intt1(1 To 1118, 1 To 12) As String '二进制数组
Dim Intt(1 To 1118, 1 To 12) As Byte '二进制数组
For i = 1 To ie - 1
Data8.Refresh
Data8.Recordset.AddNew
lxuhao.Caption = i '序号
'计费表地址
If Intt(i, 1) = 255 Or Intt(i, 2) = 255 Then
lre.Caption = "FF"
Else
If (Intt1(i, 1) = 0 And Intt1(i, 2) <> 0) Or _
(Intt1(i, 1) < 10 And Val(Intt1(i, 2)) >= 10) Then
lre.Caption = "0" & Intt1(i, 1) & Intt1(i, 2)
End If
If (Intt1(i, 2) = 0 And Intt1(i, 1) <> 0) Or _
(Intt1(i, 2) < 10 And Intt1(i, 1) >= 10) Then
lre.Caption = Intt1(i, 1) & "0" & Intt1(i, 2)
End If
If (Intt1(i, 1) = 0 And Intt1(i, 2) = 0) Then
lre.Caption = "00" & Intt1(i, 1) & Intt1(i, 2)
End If
If (Intt1(i, 1) < 10 And Intt1(i, 2) < 10) Then
lre.Caption = "0" & Intt1(i, 1) & "0" & Intt1(i, 2)
End If
If (Intt1(i, 1) >= 10 And Intt1(i, 2) >= 10) Then
lre.Caption = Intt1(i, 1) & Intt1(i, 2)
End If
End If
'卡号
If Intt(i, 3) < 16 Then
khh1 = "0" & Intt1(i, 3)
Else
khh1 = Intt1(i, 3)
End If
If Intt(i, 4) < 16 Then
khh2 = "0" & Intt1(i, 4)
Else
khh2 = Intt1(i, 4)
End If
If Intt(i, 5) < 16 Then
khh3 = "0" & Intt1(i, 5)
Else
khh3 = Intt1(i, 5)
End If
If Intt(i, 6) < 16 Then
khh4 = "0" & Intt1(i, 6)
Else
khh4 = Intt1(i, 6)
End If
lkahao.Caption = khh1 & khh2 & khh3 & khh4
'余额百元
If Intt1(i, 7) = "FF" Then
lybai.Caption = "-"
Else
lybai.Caption = Intt1(i, 7)
End If
'余额元
If Intt1(i, 8) = "FF" Then
lyyuan.Caption = "-"
Else
lyyuan.Caption = Intt1(i, 8)
End If
'余额分
If Intt(i, 9) = "255" Then
lyfen.Caption = Intt1(i, 9)
Else
If Intt1(i, 9) < 10 Then
lyfen.Caption = "0" & Intt1(i, 9)
Else
lyfen.Caption = Intt1(i, 9)
End If
End If
'消费额元
lxyuan.Caption = Intt1(i, 10)
'消费额分
If Intt(i, 11) = "255" Then
lxfen.Caption = Intt1(i, 11)
Else
If Intt1(i, 11) < 10 Then
lxfen.Caption = "0" & Intt1(i, 11)
Else
lxfen.Caption = Intt1(i, 11)
End If
End If
Data8.Recordset.Update
DoEvents
Next i
crycoming 2005-11-04
  • 打赏
  • 举报
回复
代码贴出来看看
diablobaal 2005-11-04
  • 打赏
  • 举报
回复
我试过,循环里加 doevents无用
zou19820704 2005-11-04
  • 打赏
  • 举报
回复
就是的,当循环比较大的时候,适当的释放点资源给系统是一个不错的办法
faysky2 2005-11-04
  • 打赏
  • 举报
回复
使用DoEvents语句,把控制权让给操作系统,就可以解决
for i= to ...
DoEvents
......
next
crycoming 2005-11-04
  • 打赏
  • 举报
回复
循环里加 doevents

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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