Answered Minimize WCF Console On Start Up

dwains

Member
Joined
Feb 23, 2016
Messages
14
Programming Experience
3-5
The following code does minimize as expected while in Visual Studio how ever after compiling and running the complied code from desktop it does not. Any thoughts of why

HTML:
Imports System.ComponentModel
Imports System.Net
Imports System.Runtime.InteropServices
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.Windows.Forms
'Imports ServerConsole.HostModule

Public Module NotifyIcon1    
    <DllImport("user32.dll", SetLastError:=True)>
    Private Function SetParent(hWndChild As IntPtr, hWndNewParent As IntPtr) As IntPtr
    End Function    <DllImport("user32.dll")>
    Private Function GetShellWindow() As IntPtr
    End Function    <DllImport("user32.dll")>
    Private Function GetDesktopWindow() As IntPtr
    End Function    Private ViewConsoleState As WindowShowStyle

    Private Enum WindowShowStyle As UInteger        
        Hide = 0
        ShowNormal = 1
        ShowMinimized = 2
        ShowMaximized = 3
        Maximize = 3
        ShowNormalNoActivate = 4
        Show = 5
        Minimize = 6
        ShowMinNoActivate = 7
        ShowNoActivate = 8
        Restore = 9
        ShowDefault = 10
        ForceMinimized = 11    End Enum

    Dim WithEvents notifyIcon As NotifyIcon
    Dim processHandle As IntPtr
    Dim WinShell As IntPtr
    Dim WinDesktop As IntPtr
    Dim HideMenu As MenuItem
    Dim RestoreMenu As MenuItem
    Dim RestartMenu As MenuItem
    'Dim MinimizeMenu As MenuItem

    Sub Main() 
        notifyIcon = New NotifyIcon()
        notifyIcon.Icon = My.Resources.MangagedMonitoringConsole
        notifyIcon.Text = "TattleTale External Monitor Running" & Environment.NewLine & GetLocalIPAddress()
        notifyIcon.Visible = True        Dim menu As New ContextMenu()
        HideMenu = New MenuItem("Hide Console", New EventHandler(AddressOf Minimize_Click))
        RestoreMenu = New MenuItem("Show Console", New EventHandler(AddressOf Maximize_Click))
        'MinimizeMenu = New MenuItem("Minimize Image Server", New EventHandler(AddressOf MinimizeConsole))        menu.MenuItems.Add(RestoreMenu)
        menu.MenuItems.Add(HideMenu)
        'menu.MenuItems.Add(MinimizeMenu)
        menu.MenuItems.Add(New MenuItem("Restart Image Server", New EventHandler(AddressOf RestartServer)))
        menu.MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf CleanExit)))
        notifyIcon.ContextMenu = menu
        'You need to spin off your actual work in a different thread so that the Notify Icon works correctly
        Task.Factory.StartNew(AddressOf Run)
        processHandle = Process.GetCurrentProcess().MainWindowHandle
        WinShell = GetShellWindow()
        WinDesktop = GetDesktopWindow()        'Hide the Window
        ResizeWindow(False)
        'ViewConsoleState = WindowShowStyle.ShowMinimized
        'This is required for triggering WinForms activity in Console app
        Application.Run() 
      End Sub

    Private Sub Run()
            Dim uri = New Uri("http://" & GetLocalIPAddress() & ":8080/hello")        Dim baseAddress As Uri = New Uri("http://" & GetLocalIPAddress() & ":8080/hello")        Try
            Dim Host As New ServiceHost(GetType(WcfServiceLibrary1.Service1), baseAddress)
            Dim smb As New ServiceMetadataBehavior()
            smb.HttpGetEnabled = True
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
            Host.Description.Behaviors.Add(smb)
            Host.Open()
            Console.WriteLine("The service is ready at {0}", baseAddress)
            Console.WriteLine("Press <Enter> to stop the service.")
            Console.WriteLine("Press minimize button to hide console.")
            Console.ReadLine()
            ' Close the ServiceHost.
            Host.Close()
            MinimizeConsole()        Catch commProblem As CommunicationException
            'There is already a WCF instance Running
            notifyIcon.Visible = False
            notifyIcon.Dispose()
            MsgBox("Image Server Is Already Running")
            Application.Exit()
            'Console.WriteLine("There was a communication problem. " + commProblem.Message)
            'Console.Read()
        End Try        While True
            System.Threading.Thread.Sleep(1000)
        End While
    End Sub

    Private Sub CleanExit(sender As Object, e As EventArgs)
        notifyIcon.Visible = False
        notifyIcon.Dispose()
        Application.Exit()
    End Sub

    Private Sub Minimize_Click(sender As Object, e As EventArgs)
        MinimizeConsole()
    End Sub    Private Sub Maximize_Click(sender As Object, e As EventArgs)
        ViewConsoleState = WindowShowStyle.Show
        ResizeWindow()
    End Sub

    Private Sub ResizeWindow(Optional Restore As Boolean = True)
        If Restore Then
            RestoreMenu.Enabled = False
            HideMenu.Enabled = True
            SetParent(processHandle, WinDesktop)
        Else
            RestoreMenu.Enabled = True
            HideMenu.Enabled = False
            SetParent(processHandle, WinShell)
        End If
    End Sub

    Private Sub RestartServer()
        Try
            notifyIcon.Visible = False
            notifyIcon.Dispose()
            Application.Restart()        
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try    End Sub

    Private Sub MinimizeConsole()
        ViewConsoleState = WindowShowStyle.Hide
        ResizeWindow(False)
    End Sub

    Private Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As WindowShowStyle) As Boolean
        Return ShowWindow
    End Function

    Public Function GetLocalIPAddress() As String
        Dim _IP As String = Nothing
        Dim _IPHostEntry As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())        ' IPAddress class contains the address of a computer on an IP network. 
        For Each _IPAddress As System.Net.IPAddress In _IPHostEntry.AddressList
            ' InterNetwork indicates that an IP version 4 address is expected 
            ' when a Socket connects to an endpoint
            If _IPAddress.AddressFamily.ToString() = "InterNetwork" Then
                _IP = _IPAddress.ToString()
            End If
        Next _IPAddress
        Return _IP
    End Function

End Module
 
documentation said:
A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.
Use GetConsoleWindow function to get the handle: pinvoke.net: getconsolewindow (kernel32)
 
Back
Top