大佬教程收集整理的这篇文章主要介绍了控制台应用程序在第二次调用时最小化错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
第一次运行正常,但第二次运行出错
最小化子
system.invalIdoperationException: '在单个线程上启动第二个消息循环不是有效的操作。改用 Form.ShowDialog。'
按 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
以上是大佬教程为你收集整理的控制台应用程序在第二次调用时最小化错误全部内容,希望文章能够帮你解决控制台应用程序在第二次调用时最小化错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。