关于#vb.net#在类库多线程中调用委托报错的问题,如何解决?

vb.net在类库多线程中调用委托报错,先上源码

  '一个From窗体(1个Textbox控件+2个Button控件)+ 一个Class类
Imports System.Threading
Public Class Form1
    Public Delegate Sub testDelegate(ByVal text1 As String)   '声明委托类
    Dim abc As New Class1
    Public Shared Sub AppendText(ByVal text1 As String)         '声明一个方法,给文本框添加内容 ,该方法和委托要匹配(签名一致)
        Form1.TextBox1.Text = text1                            'Me可以省略
    End Sub
    Public Shared Sub method1(ByVal text2 As String)            '声明线程调用的方法一
        'Me.Invoke(New testDelegate(AddressOf AppendText), 参数)        ’   参数被实参代替      
        Form1.Invoke(New testDelegate(AddressOf Form1.AppendText), "跨线程在控件文本框中添加内容。")
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '        CheckForIllegalCrossThreadCalls = False
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Invoke(New testDelegate(AddressOf Form1.AppendText), "跨线程在控件文本框中添加内容。")
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        abc.委托过程()
    End Sub
End Class

 '以下为Class类源码
Imports System.Threading
Imports System.Windows.Forms.Form
Public Class Class1
    Sub 委托过程()
        Debug.Print("beging")
        Dim XCheng1 As New Threading.Thread(AddressOf Form1.method1)
        XCheng1.Start()
        'Form1.Invoke(New Form1.TDWt(AddressOf Form1.TD_Wt), Form1.Label1, "text", Form1.TextBox1.Text)
        Debug.Print("end")
    End Sub
End Class

运行结果,Button1调用委托正常,Button2调用类库中的多线程委托,提示窗口未加载。详情报错如下
System.InvalidOperationException
HResult=0x80131509
Message=在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。
Source=System.Windows.Forms
StackTrace:
在 System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
在 System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
在 WindowsApp3.Form1.method1(String text2) 在 C:\Users\Administrator\source\repos\WindowsApp3\WindowsApp3\Form1.vb 中: 第 12 行
在 WindowsApp3.Class1._Closure$__._Lambda$__R1-1(Object a0)
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart(Object obj)

此异常最初是在此调用堆栈中引发的:
System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, object[], bool)
System.Windows.Forms.Control.Invoke(System.Delegate, object[])
WindowsApp3.Form1.method1(String) (位于 Form1.vb 中)
WindowsApp3.Class1._Closure$__._Lambda$__R1-1(object)
System.Threading.ThreadHelper.ThreadStart_Context(object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
System.Threading.ThreadHelper.ThreadStart(object)

分别尝试把委托写在类中调用,报同样的错误。

各位麻烦帮忙看一下,应该如何改正,或者达到在类的多线程中能改变其它窗体中控件的相关属性的功能。

根據錯誤訊息,找到的鏈接:
"在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke"_浊酒入清梦的博客-CSDN博客_在创建窗口之前
https://blog.csdn.net/m0_37862405/article/details/78387226

另外,Dim abc As New Class1
abc 需不需要做一些 InitializeComponent 的動作?

看一下控件的InvokeRequired 属性。不一定需要Invoke方法。


WinForms中的控件绑定到特定线程,并且不是线程安全的。 因此,如果要从其他线程调用控件的方法,则必须使用控件的调用方法之一来封送对正确线程的调用。 此属性可用于确定是否必须调用调用方法,如果不知道线程拥有哪个控件,这非常有用。