拖动表单错误

时间:2017-07-15 07:11:06

标签: vb.net

我正在创建一个具有编写微控制器eeprom功能的应用程序,我发送512个字符串字符,它写得很好并且没有任何错误,但是当我通过串行发送值时拖动表单,我有写错误。好像当我拖动窗体并按住鼠标左键时,所有其他cmds停止工作,并在之前的cmd中拖动窗体。怎么解决?我在发送序列之前尝试过:

  My.Application.DoEvents()

没有多大帮助。

Private Sub erom_write_Click(sender As Object, e As EventArgs) Handles erom_write.Click
    RichTextBox3.Text = ""
    Label6.Text = "/"
    Label7.Text = (key_erom.Length) / 2
    OK_rs = 0
    ProgressBar1.Maximum = key_erom.Length
    ProgressBar1.Minimum = 0
    Call CHeck_CB_Click(sender, e)
    Dim BytesToSend(3) As Byte
    Dim BytesToSend1(1) As Byte
    Dim count As Short
    Dim page As Short

    BytesToSend1(0) = Hex(84)       ' for call write
    BytesToSend1(1) = 0 ' dummy
    SerialPort1.Write(BytesToSend1, 0, BytesToSend1.Length)
    'Threading.Thread.Sleep(700)
    'Threading.Thread.Sleep(10)
    page = 0
    For count = 1 To RichTextBox2.Text Step 2  'For count = 249 To Text4.Text * 2 Step 8 page 32
        BytesToSend(0) = (page And &HFF00&) / &H100
        BytesToSend(1) = (page And &HFF&)
        BytesToSend(2) = CLng("&H" & Mid(key_erom, count, 2))
        My.Application.DoEvents()
        If count < RichTextBox2.Text - 1 Then
            BytesToSend(3) = 1
        Else
            BytesToSend(3) = 0
        End If
        'Threading.Thread.Sleep(1)
        SerialPort1.Write(BytesToSend, 0, BytesToSend.Length)
        My.Application.DoEvents()
        Threading.Thread.Sleep(7)
        If RichTextBox3.Text = "000001020304" Then  'this we get from ic if error write
            Label1.Text = "ERROR"
            ProgressBar1.Value = ProgressBar1.Minimum
            ProgressBar1.Visible = False
            Label5.Visible = False
            Label7.Visible = False
            check_ckicl = 0
            Exit Sub
        End If
        'Threading.Thread.Sleep(300)
        page = page + 1
            Label5.Text = Math.Round(count / 2)
            ProgressBar1.Value = count

    Next
    Label5.Text = ""
    Label7.Text = ""
    Label6.Text = ""
    My.Application.DoEvents()
    ProgressBar1.Visible = False
    ProgressBar1.Value = 0

更新:

       Private Sub CHeck_CB_Click(sender As Object, e As EventArgs) Handles 
       CHeck_CB.Click
       If ToolStripComboBox2.Text = "45/52/53" Then
        Call Button2_Click(sender, e)
      End If
      If ToolStripComboBox2.Text = "41/61" Then
        Call boot_61_Click(sender, e)
    End If
  End Sub

1 个答案:

答案 0 :(得分:0)

您应该使用多线程在拖动表单时更新UI控件。

首先将所有内容从 erom_write_Click 移动到新方法,假设 UpdateValuesInControl 。所以它会像:

Sub UpdateValuesInControl
  RichTextBox3.Text = ""
  Label6.Text = "/"
  Label7.Text = (key_erom.Length) / 2
  OK_rs = 0
  ProgressBar1.Maximum = key_erom.Length
  ProgressBar1.Minimum = 0

  'You don't have to write "Call"
  'Call CHeck_CB_Click(sender, e)

  'You can simply write
   CHeck_CB.BeginInvoke(Sub() 
                    CHeck_CB_Click(Nothing, Nothing)
                    End Sub)

   '
   '
   '
   '
   '
   '
   '
  'And so on, add the rest of the code in this method
  'DO NOT EVER USE "Application.DoEvent"
End Sub

现在在点击它时启动一个线程,它将调用 UpdateValuesInControl ,如:

Private Sub erom_write_Click(sender As Object, e As EventArgs) Handles erom_write.Click
     'Below mentioned codes will start a new thread from the main one
     'Thread Class is in System.Threading namespace
     'So write "Imports System.Threading" in the namespace area in your code, which is at the top-most position.
     Dim work As New Thread(AddressOf UpdateValuesInControl) 'This will create a second thread
     work.IsBackground = True
     work.Start()
     'Because UpdateValuesInControl is going to run in a second thread you main thread won't freeze while dragging event is called.
End Sub

现在,对于必须更新状态或显示UI中现有值的更改的每个控件,请调用 Invoke BeginInvoke 来执行相同操作。以下是在完成拖动时需要更新的单个控件Invoke方法的示例。

Sub UpdateValuesInControl
'
'
 Label7.BeginInvoke(Sub() 
                      Label7.Text = (key_erom.Length) / 2
                    End Sub)

'
'
'
'
End Sub

注意:您需要为每个需要更新的控件调用Invoke或BeginInvoke

此外,如果您开始命名您的UI控件,这将是一个很好的做法:

  

lblStatus而不是Label7

这只会让你自己知道每个控件的下落