简体   繁体   中英

vb console application does not close

I created a vb.net to grab an attribute from AD and create a local variable. It does that just fine, the problem is that it leaves the console window open with nothing but a flashing cursor. Ideally it would close as soon as it created the variable.

I've tried adding Environment.Exit (0)

Module Module1

Sub Main()
    Dim objShell
    Dim objUserEnv
    Dim objADSysInfo
    Dim objUser

    objShell = CreateObject("WScript.Shell")
    objUserEnv = objShell.Environment("USER")
    objADSysInfo = CreateObject("ADSystemInfo")
    objUser = GetObject("LDAP://" & objADSysInfo.UserName)
    ' This will create the variable %ipphone%
    objUserEnv("ipphone") = objUser.ipPhone

End Sub

End Module

Any ideas why it's not closing the console?

Module MyApp

Sub Main()
    ' Attach the event handler method
   AddHandler AppDomain.CurrentDomain.ProcessExit, AddressOf MyApp_ProcessExit

   Dim objShell
   Dim objUserEnv
   Dim objADSysInfo
   Dim objUser

   objShell = CreateObject("WScript.Shell")
   objUserEnv = objShell.Environment("USER")
   objADSysInfo = CreateObject("ADSystemInfo")
   objUser = GetObject("LDAP://" & objADSysInfo.UserName)
   ' This will create the variable %ipphone%
    objUserEnv("ipphone") = objUser.ipPhone

    Environment.Exit(0)
End Sub

Private Sub MyApp_ProcessExit(sender As Object, e As EventArgs)
    Console.WriteLine("App Is Exiting...")
End Sub

End Module

Try this :

Application.Exit

I think it's cause of the settings of your project, I never had such a matter

Ok, Since Environment.Exit(0) is not working and you are within a Console application, Try this,

Go to Project > Add Reference > Assemblies > Framework and search for System.Windows.Forms click the check box and then click the Ok after that add this to your code.

Imports System.Windows.Forms
Module Module1

Sub Main()

    Dim objShell
    Dim objUserEnv
    Dim objADSysInfo
    Dim objUser

    objShell = CreateObject("WScript.Shell")
    objUserEnv = objShell.Environment("USER")
    objADSysInfo = CreateObject("ADSystemInfo")
    objUser = GetObject("LDAP://" & objADSysInfo.UserName)
    ' This will create the variable %ipphone%
    objUserEnv("ipphone") = objUser.ipPhone
    Application.Exit()
End Sub
End Module

Another reason that it might not be closing is because it is not completing the task, When I ran your code, It didnt complete and I got this error

Additional information: No mapping between account names and security IDs was done. (Exception from HRESULT: 0x80070534)

Since you didn't mention this in your question, Im guessing you didnt get this error?

原来我只是不耐烦,而且确实有足够的时间才能关闭。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM