Help!
I am trying to programatically enable and disable LAN connections. The code works just fine, BUT it hangs. All of the code is below. It is stripped down to perform only one function (turn off the LAN connection). How can I get this to end normally? I am interested because I ultimately want the "main" routine to be a function returning a code.
I've tried set objects to "nothing". I've tried moving the code to a thread and killing the thread. I've tried making it a separate class. I've even tried to put it in a Windows form and run it as an application. Nothing seems to keep it from hanging.
HELP!
Module Module1
Public Sub Main()
process1()
' the task will not end here, just hangs
Exit Sub
End Sub
Private Sub process1()
Const ssfCONTROLS = 3
Dim ConnectionName As String = "Local Area Connection"
Dim DisableVerb As String = "Disa&ble"
Dim ShellApp As New Shell32.Shell
Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS)
Dim FolderItem As Shell32.FolderItem
Dim NetworkFolder As Shell32.Folder
Dim LANConnection As Shell32.FolderItem
For Each FolderItem In ControlPanel.Items()
If FolderItem.Name = "Network and Dial-up Connections" Then
NetworkFolder = FolderItem.GetFolder
Exit For
End If
Next
If NetworkFolder Is Nothing Then
Exit Sub
End If
For Each FolderItem In NetworkFolder.Items()
If FolderItem.Name = ConnectionName Then
LANConnection = FolderItem
Exit For
End If
Next
If LANConnection Is Nothing Then
Exit Sub
End If
Dim DisableVerbItem, Verb As Shell32.FolderItemVerb
For Each Verb In LANConnection.Verbs
If Verb.Name = DisableVerb Then
DisableVerbItem = Verb
End If
Next
DisableVerbItem.DoIt()
End Sub
End Module
I am trying to programatically enable and disable LAN connections. The code works just fine, BUT it hangs. All of the code is below. It is stripped down to perform only one function (turn off the LAN connection). How can I get this to end normally? I am interested because I ultimately want the "main" routine to be a function returning a code.
I've tried set objects to "nothing". I've tried moving the code to a thread and killing the thread. I've tried making it a separate class. I've even tried to put it in a Windows form and run it as an application. Nothing seems to keep it from hanging.
HELP!
Module Module1
Public Sub Main()
process1()
' the task will not end here, just hangs
Exit Sub
End Sub
Private Sub process1()
Const ssfCONTROLS = 3
Dim ConnectionName As String = "Local Area Connection"
Dim DisableVerb As String = "Disa&ble"
Dim ShellApp As New Shell32.Shell
Dim ControlPanel As Shell32.Folder = ShellApp.NameSpace(ssfCONTROLS)
Dim FolderItem As Shell32.FolderItem
Dim NetworkFolder As Shell32.Folder
Dim LANConnection As Shell32.FolderItem
For Each FolderItem In ControlPanel.Items()
If FolderItem.Name = "Network and Dial-up Connections" Then
NetworkFolder = FolderItem.GetFolder
Exit For
End If
Next
If NetworkFolder Is Nothing Then
Exit Sub
End If
For Each FolderItem In NetworkFolder.Items()
If FolderItem.Name = ConnectionName Then
LANConnection = FolderItem
Exit For
End If
Next
If LANConnection Is Nothing Then
Exit Sub
End If
Dim DisableVerbItem, Verb As Shell32.FolderItemVerb
For Each Verb In LANConnection.Verbs
If Verb.Name = DisableVerb Then
DisableVerbItem = Verb
End If
Next
DisableVerbItem.DoIt()
End Sub
End Module