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