Vb.net 拖动表单错误
我正在制作一个应用程序,它具有写入微控制器eeprom的功能,我发送512个字符串字符,它写得很好,没有任何错误,但是当我在通过串行发送值时拖动表单时,我有一个写入错误。似乎当我拖动表单并按住鼠标左键时,所有其他cmd都停止工作,而之前的拖动表单为cmd。如何修复它?我在发送序列号之前尝试过:Vb.net 拖动表单错误,vb.net,Vb.net,我正在制作一个应用程序,它具有写入微控制器eeprom的功能,我发送512个字符串字符,它写得很好,没有任何错误,但是当我在通过串行发送值时拖动表单时,我有一个写入错误。似乎当我拖动表单并按住鼠标左键时,所有其他cmd都停止工作,而之前的拖动表单为cmd。如何修复它?我在发送序列号之前尝试过: My.Application.DoEvents() 没有什么帮助 Private Sub erom_write_Click(sender As Object, e As EventArgs) Han
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
拖动表单时,应使用多线程更新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,如下所示:
现在,对于必须在UI中更新状态或显示现有值更改的每个控件,调用Invoke或BeginInvoke执行相同的操作。下面是一个单一控件调用方法的示例,该方法需要在执行拖动时更新
Sub UpdateValuesInControl
'
'
Label7.BeginInvoke(Sub()
Label7.Text = (key_erom.Length) / 2
End Sub)
'
'
'
'
End Sub
注意:您必须为每个需要更新的控件调用Invoke或BeginInvoke
此外,如果您开始将UI控件命名为:
lblStatus而不是Label7
这只会帮助您了解每个控件的位置删除My.Application.DoEvents()。如果需要实时更新UI控件中的值,请使用适当的Invoke/BeginInvoke方法执行多线程。我尝试了多次,但这些方法对我来说似乎很难,我最近从vb6获得了im。还有,为什么拖拽表单会打断我当前的方法??如果发生错误,那么使用Application.Doevents()将很难调试代码。在.NET相关语言中,您不应该使用它,因为它的存在只是为了与VB6向后兼容。因此,删除它并调试,然后检查错误。为什么不使用线程异步写入eeprom?这将确保UI中的任何内容都不会影响其执行!我删除了所有的doevents,你是对的,我不能在窗体执行其他事件时拖动它。所以一切都很好,没有错误。你能做一个建议方法的例子,而不是一个解决方案,如果它有助于我接受它为什么在Sub UpdateValuesInControl中,检查\u CB\u Click(发送方,e)-发送方有红线错误“它说了一些关于保护级别的事情此单击事件看起来像:私有子检查\u CB\u Click(发送方作为对象,e作为事件参数)处理CHeck_CB.ClickIn CHeck_CB_Click(sender,e)是否使用此sender对象和e事件?如果不使用,则可以不传递任何内容。检查我的更新答案CHeck_CB_Click是一个按钮,如果我不使用任何内容而使用sender和e,则我有错误参数没有参数sender和e on CHeck_CB_Click(sender作为对象,e作为事件参数)你能发布检查点击(发送者,e)点击事件吗
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
Sub UpdateValuesInControl
'
'
Label7.BeginInvoke(Sub()
Label7.Text = (key_erom.Length) / 2
End Sub)
'
'
'
'
End Sub