我有两种形式:“主要”和“面板”。
主窗体包含 2 个按钮:“bt_panel1”和“bt_panel2”。
每个按钮的功能是:
Private Sub bt_panel1_Click(sender As System.Object, e As System.EventArgs) Handles bt_panel1.Click
Dim panel1_run As New panel
panel1_run.PANEL_NUM = 1
panel1_run.Name = "panel1_run"
panel1_run.Text = "PANEL 1"
panel1_run.Show()
End Sub
(关于 bt_panel2,“2”而不是“1”)
面板形式包含:
- 函数加载:
Private Sub panel_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
[...]
'create 80 picturboxes
For i = 1 To 4
For k = 1 To 20
Dim picture As New PictureBox
picture.Name = "pic" & i & "c" & k
[...]
Controls.Add(picture)
AddHandler picture.Paint, AddressOf paintchar
Next
Next
'start BackgroundWorker
BackgroundWorker1.RunWorkerAsync()
End Sub
- 一个后台工作者控件(它做一些不相关的事情)及其“RunWorkerCompleted”:
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Dim temp As PictureBox
Try
For i = 1 To 4
For k = 1 To 20
'for each picturebox call refresh (paintchar)
temp = Me.Controls.Item("pic" & i & "c" & k)
temp.Refresh()
Next
Next
'restart BackgroundWorker1 continuosly
BackgroundWorker1.RunWorkerAsync()
Catch ex As Exception
MsgBox("Error BackgroundWorker1_RunWorkerCompleted" & vbCrLf & ex.Message)
End Try
End Sub
- 功能涂料:
Private Sub paintchar(sender As System.Object, e As System.Windows.Forms.PaintEventArgs)
[...]
Try
e.Graphics.FillEllipse(Brushes.LawnGreen, New RectangleF(loc, size))
Catch ex As Exception
MsgBox("Error paintchar" & vbCrLf & ex.Message)
End Try
End Sub
如果我只打开一个面板(在 Main 中按 bt_panel1 或 bt_panel2),一切正常。
但是,如果我打开两个面板(在 Main 中按 bt_panel1 和 bt_panel2),应用程序没有响应,并且 panel2 完全空白(未绘制)。
应用程序没有响应(我无法关闭面板,也无法关闭 Main...我必须终止调试才能关闭)。
不调用异常。
未使用 ReportProgress 函数。
DoWork 函数从 SharedMemory 区域读取一些数据(panel1 和 2 的数据不同)
我认为问题在于 RunWorkerCompleted 中的“Me.Controls”没有引用到表单的相应实例。