控制台应用程序在第二次调用时最小化错误
console app minimize error the second time it's called
第一次运行运行良好,第二次出错
Application.Run() 在 minimized sub
中出现错误
System.InvalidOperationException: 'Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.'
按 f5 将控制台最小化到带有 notiflyicon 的系统托盘
单击 notiflyicon 使控制台恢复正常大小
(重复两次会报错)
这是代码(控制台应用程序 .net 框架)
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
这是有效的代码(上下文菜单)
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
第一次运行运行良好,第二次出错
Application.Run() 在 minimized sub
System.InvalidOperationException: 'Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead.'
按 f5 将控制台最小化到带有 notiflyicon 的系统托盘
单击 notiflyicon 使控制台恢复正常大小
(重复两次会报错)
这是代码(控制台应用程序 .net 框架)
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
这是有效的代码(上下文菜单)
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