0

first time run is working well, but the second time is error
error is on Application.Run() in minimized sub

System.InvalidOperationException: 'Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.'

press f5 to minimize the console to system tray with notiflyicon
click notiflyicon to get console back to normal size
(do it twice and you will get the error)

here is code (console app .net framework)

Imports System
Imports System.Windows.Forms

Module Module1
    Public Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    Public Declare Auto Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
    Public Const SW_HIDE As Integer = 0
    Public hWndConsole As IntPtr

    Public notifyIcon As New NotifyIcon

    Public Sub minimized()
        While (True)
            Dim keyinfo As New ConsoleKeyInfo
            keyinfo = Console.ReadKey()
            If keyinfo.Key = 116 Then
                hWndConsole = GetConsoleWindow()
                ShowWindow(hWndConsole, 0)
                notifyIcon.Icon = My.Resources.ock
                notifyIcon.Text = "notifyIcon text"
                notifyIcon.Visible = True
                AddHandler notifyIcon.MouseClick, AddressOf OnIconMouseClick
                Application.Run() '<<<<<<<<<<< second time ERROR HERE
            End If
        End While
    End Sub

    Public Sub Main()
        Console.WriteLine("test")
        minimized()

        Console.ReadLine()
    End Sub

    Public Sub OnIconMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            notifyIcon.Visible = False
            ShowWindow(hWndConsole, 1)
            minimized()
        End If
    End Sub

End Module
DREAM
  • 426
  • 2
  • 14
  • Follow this pattern: [Adding MenuItems to Contextmenu for a TrayIcon in a Console app](https://stackoverflow.com/a/65048753/7444103) – Jimi Apr 28 '21 at 21:08
  • thank you, now i can do it with context menu! – DREAM Apr 29 '21 at 13:49

1 Answers1

0

here is the code that working (context menu)

Imports System
Imports System.Windows.Forms
Imports System.Timers
Imports System.Threading
Imports System.Drawing

Module Module1
    Public Declare Auto Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
    Public Declare Auto Function GetConsoleWindow Lib "kernel32.dll" () As IntPtr
    Public Const SW_HIDE As Integer = 0
    Public hWndConsole As IntPtr

    Public flow_time As System.Timers.Timer

    Public notifyIcon As New NotifyIcon
    Public notifycontext As New ContextMenu

    Public menu_show As New MenuItem("Show")
    Public menu_hide As New MenuItem("Hide")

    Public Sub Run_time()
        flow_time = New System.Timers.Timer(1000)
        AddHandler flow_time.Elapsed, AddressOf OnTimedEvent
        flow_time.AutoReset = True
        flow_time.Enabled = True
    End Sub

    Public Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        flow_time.Enabled = False

        notifyIcon.Icon = SystemIcons.Application
        notifyIcon.Visible = True
        notifyIcon.ContextMenu = notifycontext

        notifycontext.MenuItems.Add(menu_hide)

        AddHandler menu_show.Click, AddressOf show_console
        AddHandler menu_hide.Click, AddressOf hide_console

        Application.Run()
    End Sub

    Public Sub Main()
        Run_time()
        For i As Integer = 1 To 200
            Console.WriteLine("{0} ", i)
            Threading.Thread.Sleep(1000)
        Next
        Console.ReadLine()
    End Sub

    Public Sub show_console()
        notifycontext.MenuItems.Remove(menu_show)
        notifycontext.MenuItems.Add(menu_hide)

        hWndConsole = GetConsoleWindow()
        ShowWindow(hWndConsole, 1)
    End Sub

    Public Sub hide_console()
        notifycontext.MenuItems.Remove(menu_hide)
        notifycontext.MenuItems.Add(menu_show)

        hWndConsole = GetConsoleWindow()
        ShowWindow(hWndConsole, 0)
    End Sub

End Module
DREAM
  • 426
  • 2
  • 14