首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

Invoke有关问题-模块中代码调用Form中代码的有关问题

2012-09-02 
【求助】Invoke问题-模块中代码调用Form中代码的问题如下的代码存在form中:------------------------VB.NET

【求助】Invoke问题-模块中代码调用Form中代码的问题
如下的代码存在form中:
------------------------

VB.NET code
Public Class Form1    '添加到Listview    Function AddToListView(ByVal id As Integer, ByVal type As String, ByVal value As String, ByVal path As String) As Boolean        Dim s As New ListViewItem        s.Text = id.ToString        s.SubItems.Add(type)        s.SubItems.Add(value)        s.SubItems.Add(path)        ListView1.Items.Add(s)        AddToListView = True    End Function    Private Delegate Function vL(ByVal id As Integer, ByVal type As String, ByVal value As String, ByVal path As String) As Boolean    Sub goodme(ByVal id As Integer, ByVal type As String, ByVal value As String, ByVal path As String)        Do While Me.IsHandleCreated = False            Application.DoEvents()            Debug.Print("trad")        Loop        Me.Invoke(New vL(AddressOf AddToListView), id, type, value, path)    End Sub    Private Sub bFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bFind.Click        Dim i As New Threading.Thread(AddressOf good)        i.Start()    End Sub    Sub good()        Call goodme(1, "dir", "1.txt", "C:\1.txt")        Debug.Print("ok")    End SubEnd Class

---------------------
上面的代码可以正常执行。
上面的情况是:
Delegate在Form中
被Sub调用
而sub被function调用
且function也位于Form中



但是,如果其他的都不改变,将sub good移到一个模块中,然后在Form中启动一个线程(with AddressOf good),在这个线程中去调用这个Form1中的goodme函数,就会出现“在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke”的错误。

[解决办法]
你是不是又实例化了一个Form1?

热点排行