0

以前にこの問題に遭遇し、以前に質問したことがあるため、このスレッドは重複としてマークされました。私は通常、それを解決する方法を知っています (つまり、名前の代わりにフォームのインスタンスを指す変数を使用することにより)。ただし、今回は参照する必要があるデフォルトの起動フォームです。そのため、そのインスタンスを格納する変数はなく、変数なしで既存のインスタンスを参照する方法がわかりません。

2 つのフォーム (frmMain と frmStatus) があります。frmStatus のインスタンスを作成し、それを frmMain の statusDialog 変数に格納します。次に、(ADSearcher クラス用に) 新しいスレッドを作成します。このスレッドはイベントを発生させ、デリゲートを使用して statusDialog のサブルーチンを呼び出します。ADSearcher スレッドは、他の複数のスレッド (私の ComputerSearcher スレッド用) も作成します。これらのスレッドも、デリゲートを使用して statusDialog でサブルーチンを呼び出すイベントを発生させる必要があります。

問題は ComputerSearcher スレッドにあります。AddHandler 行が frmMain の新しいインスタンスを作成しているように見えます。これは、statusDialog が Nothing/Null であることを意味します。AddHandler がフォームの新しいインスタンスを作成するのはなぜですか?正しいインスタンスを使用する方法はありますか?

新しいインスタンスを作成していると思う理由は、コードをステップ実行すると、最初の AddHandler 行が実行され、frmMain の上部 (サブルーチンの外側) で変数宣言がステップ実行され、その時点で statusDialog が Nothing になるためです。

出発点は Private Sub mnuSync_ADSync_Click(...) です。これにより、最初のスレッドとクラス インスタンスが作成されます。膨大なエッセイを投稿しなくても、何が起こっているかを確認できるように、十分なコードを提供しようとしました。追加のコードが必要な場合は、お問い合わせください。

前もって感謝します、

fromMain

Dim statusDialog As frmStatus 'Used to create an instance of the status dialog

Private Sub mnuSync_SyncAD_Click(sender As Object, e As EventArgs) Handles mnuSync_SyncAD.Click
    With adSync
        .RootPath = "LDAP://domain.com"
        .FilterString = "(objectCategory=organizationalUnit)"

        '### TO DO: Replace .UserID and .Password values with a stored value
        If Not integratedAuth Then
            .UserID = "...."
            .Password = "...."
        End If
        .PageSize = 5
        .PropertiesToLoad = New String() {"cn", "name", "distinguishedName", "dNSHostName", "objectCategory", "objectGUID"}

        'Create the status dialog
        statusDialog = New frmStatus

        ThreadPool.QueueUserWorkItem(AddressOf .StartSearch)

        statusDialog.ShowDialog()

    End With
End Sub

'[ADSearcher Events]
Private Delegate Sub displayOUStatus(ByVal ousFound As Integer)
Private Sub adSync_OUResultFound(ByVal ousFound As Integer) Handles adSync.OUResultFound
    Dim updateStatus As New displayOUStatus(AddressOf statusDialog.UpdateOUSearcherStatus)
    Me.Invoke(updateStatus, New Object() {ousFound})
End Sub

Private Delegate Sub displayResult(ByVal node As TreeNode)
Private Delegate Sub findMACAddresses(ByVal compCollection As ComputerCollection)
Private Sub adSync_SearchCompleted(ByVal ouNodes As TreeNode) Handles adSync.SearchCompleted
    Dim display As New displayResult(AddressOf AddOUToTreeView)
    Me.Invoke(display, New Object() {ouNodes})

    'Check for any computers which are no longer in Active Directory
    For Each compComparison As String In compGUIDComparison
        Dim query = From comp As Computer In computers Where comp.GUID = compComparison Select comp

        'If this computer is no longer in Active Directory remove it
        If query.Count > 0 Then
            computers.Remove(query(0))
        End If
    Next

    Dim macAddresses As New findMACAddresses(AddressOf GetMacAddresses)
    Me.Invoke(macAddresses, New Object() {computers})

End Sub
'[/ADSearcher Events]

'[ComputerSearcher Events]
Private Delegate Sub displayCompStatus(ByVal pcsFound As Integer)
Public Sub adSync_CompResultFound(ByVal pcsFound As Integer)
    Dim updateStatus As New displayCompStatus(AddressOf statusDialog.UpdateCompSearcherStatus)
    Me.Invoke(updateStatus, New Object() {pcsFound})
End Sub

Private Delegate Sub newComputer(ByVal computer As Computer)
Public Sub adSync_ComputerFound(ByVal name As String, ByVal fqdn As String, ByVal path As String, ByVal guid As String)
    Dim computer As New Computer
    computer.Name = name
    computer.FQDN = fqdn
    computer.Path = path
    computer.GUID = guid

    compGUIDComparison.Add(guid)

    Dim query = From comp As Computer In computers Where comp.GUID = computer.GUID Select comp

    If query.Count <= 0 Then 'If the computer hasn't already been discovered add it to the collection

        If Me.InvokeRequired Then
            Dim pc As New newComputer(AddressOf Me.computers.Add)
            Me.Invoke(pc, computer)
        Else
            computers.Add(computer)
        End If

    End If

End Sub
'[/ComputerSearcher Events]

ADSearcher クラス

Sub New()
    SearchScope = DirectoryServices.SearchScope.OneLevel

    'Create reset events for the ComputerSearcher class threads
    For i As Integer = 0 To compSearchThreads.Count - 1
        compSearchThreads(i) = New ManualResetEvent(True)
    Next

End Sub

<MTAThread()>
Public Sub StartSearch()

#If Not Debug Then
    Try
#End If

    Dim rootEntry As New DirectoryEntry(RootPath)
    Dim rootNode As New TreeNode(rootEntry.Name)
    rootNode.Name = rootEntry.Path

    If Not IntegratedAuthentication Then
        rootEntry.Username = UserID
        rootEntry.Password = Password
    End If

    Dim searcher As New DirectorySearcher(rootEntry)
    searcher.PropertiesToLoad.AddRange(PropertiesToLoad)
    searcher.SearchScope = SearchScope
    searcher.PageSize = PageSize
    searcher.ServerTimeLimit = New TimeSpan(0, 10, 0)
    searcher.Filter = FilterString

    Dim queryResults As SearchResultCollection
    queryResults = searcher.FindAll()

    Dim result As SearchResult
    For Each result In queryResults

        Dim freeThread As Integer = WaitHandle.WaitAny(compSearchThreads)

        compSearchInstances(freeThread) = New ComputerSearcher(compSearchThreads(freeThread))

        With compSearchInstances(freeThread)
            .FilterString = "(objectCategory=computer)"
            If Not IntegratedAuthentication Then
                .UserID = UserID
                .Password = Password
            End If
            .PageSize = PageSize
            .SearchScope = SearchScope
            .PropertiesToLoad = PropertiesToLoad
        End With

        ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf compSearchInstances(freeThread).FindComputers), result)

        Dim childNode As New TreeNode(CStr(result.Properties("name")(0)))
        childNode.Name = result.Path
        childNode.Tag = result.Properties("objectGUID")(0)
        rootNode.Nodes.Add(SearchSub(result, childNode))

        ouResultCount += 1
        RaiseEvent OUResultFound(ouResultCount)
    Next

    WaitHandle.WaitAll(compSearchThreads)
    RaiseEvent SearchCompleted(rootNode)
    ouResultCount = 0 'Reset the result count


#If Not Debug Then
    Catch Ex as Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
#End If

ComputerSearcher クラス

Sub New(ByVal doneEvent As ManualResetEvent)
    _doneEvent = doneEvent

    'Add Event Handler
    AddHandler Me.ComputerFound, AddressOf frmMain.adSync_ComputerFound
    AddHandler Me.IncrementCompResult, AddressOf frmMain.adSync_CompResultFound
End Sub

Public Sub FindComputers(ByVal parent As SearchResult)
#If Not Debug Then
    Try
#End If

    _doneEvent.Reset() 'Signal that the thread is working

    Dim subEntry As New DirectoryEntry(parent.Path)

    If Not IntegratedAuthentication Then
        subEntry.Username = UserID
        subEntry.Password = Password
    End If

    Dim searcher As New DirectorySearcher(subEntry)
    searcher.PropertiesToLoad.AddRange(PropertiesToLoad)
    searcher.SearchScope = SearchScope
    searcher.PageSize = PageSize
    searcher.ServerTimeLimit = New TimeSpan(0, 10, 0)
    searcher.Filter = FilterString

    Dim queryResults As SearchResultCollection
    queryResults = searcher.FindAll()

    Dim result As SearchResult
    For Each result In queryResults
        pcResultCount += 1

        Dim dNSHostName As String
        If result.Properties.Contains("dNSHostName") Then 'If the computer object has a value in dNSHostName (FQDN) store it else store the basic name
            dNSHostName = result.Properties("dNSHostName")(0)
        Else
            dNSHostName = result.Properties("name")(0)
        End If

        RaiseEvent ComputerFound(result.Properties("name")(0), dNSHostName, result.Path, result.Properties("objectGUID")(0).ToString)
        RaiseEvent IncrementCompResult(pcResultCount)

    Next

    _doneEvent.Set() 'Signal that the thread is finished

#If Not Debug Then
    Catch Ex as Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical)
    End Try
#End If

End Sub
4

1 に答える 1

0

次の 2 行を置き換えます。

AddHandler Me.ComputerFound, AddressOf frmMain.adSync_ComputerFound
AddHandler Me.IncrementCompResult, AddressOf frmMain.adSync_CompResultFound

と:

AddHandler Me.ComputerFound, AddressOf My.Forms.frmMain.adSync_ComputerFound
AddHandler Me.IncrementCompResult, AddressOf My.Forms.frmMain.adSync_CompResultFound

これは、VB の既定のフォーム インスタンスのもう 1 つの例であり、開発者を悩ませています。

于 2014-09-25T21:56:51.420 に答える