我做了一个程序,我把5500个字节的数据放到一个数组里面,然后用一个fornext的循环,循环写500条记录,每条记录11个字节,在写记录时cpu占用率100%,如何解决??

解决方案 »

  1.   

    使用DoEvents语句,把控制权让给操作系统,就可以解决
    for i= to ...
       DoEvents
       ......
    next
      

  2.   

    我试过,循环里加 doevents无用
      

  3.   

    代码如下: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
      

  4.   

    把  DoEvents移到For i = 1 To ie - 1下面试试
      

  5.   

    你把DoEvents放最后肯定没有效果,那样跟不放完全一样,要放到For 下面的第一行
      

  6.   

    说错,不行你在代码中间再多加一个DoEvents,就在 '余额百元 下一行加上再试
      

  7.   

    一个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
      

  8.   

    同意楼上,doevents只是转让控制权,以便让操作系统处理其它的事件。而sleep才是挂起当前线程的操作。