.Net 应用程序在启动时添加时挂起
我使用 VB.NET 创建了一个小型多线程应用程序。用户手动运行该应用程序时没有问题。当我在启动时添加此应用程序时出现问题。我重新启动系统后它挂起。该应用程序仍在运行其线程,但我看不到它的 GUI,因为它已冻结。如果我在任务管理器上杀死它并重新启动,该应用程序就可以正常工作。该应用程序在启动时添加时挂起的可能原因是什么?
Imports System.Threading
Public Class USBLock
Public Event Lock()
Public Event Unlock()
Dim monitorThread As Thread
Public Sub StartMonitoring()
monitorThread = New Thread(AddressOf MonitorNow)
monitorThread.Start()
End Sub
Public Sub StopMonitoring()
Try
monitorThread.Abort()
Catch ex As Exception
End Try
GC.Collect()
End Sub
Private Sub MonitorNow()
'LOOP HERE
If xValid Then
' Enable Block Keyboard
' Hides Taskbar
' Disables Task Manager
RaiseEvent Unlock()
Else
' Disable Block Keyboard
' Shows Taskbar
' Enables Task Manaer
RaiseEvent Lock()
End If
Thread.Sleep(1000)
GC.Collect()
MonitorNow()
End Sub
End Class
Imports System.Reflection
Imports System.IO
Public Class frmMain
Friend WithEvents xMonitor As New USBLock
Dim xCommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim xState As Boolean = False
Dim xFrmLock As Boolean
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xFrmLock = False
userEnd = False
If Not (xCommandLineArgs.Contains("-s") Or xCommandLineArgs.Contains("-S")) Then
MyBase.WindowState = FormWindowState.Normal
End If
End Sub
Public Sub New()
MyBase.New()
InitializeComponent()
nIcon.Visible = True
xCommandLineArgs = My.Application.CommandLineArgs
If xCommandLineArgs.Contains("-s") Or xCommandLineArgs.Contains("-S") Then
nIcon.Icon = Drawing.Icon.FromHandle(CType(imgIcon.Images(1), Bitmap).GetHicon())
MyBase.Visible = False
MyBase.WindowState = FormWindowState.Minimized
StartMonitor()
Else
nIcon.Icon = Drawing.Icon.FromHandle(CType(imgIcon.Images(3), Bitmap).GetHicon())
End If
End Sub
Private Sub lockPC() Handles xMonitor.Lock
If Not xFrmLock Then
frmLock.Show()
xFrmLock = True
nIcon.ShowBalloonTip(500, "Key Not Found", "PC has been locked!", ToolTipIcon.Error)
End If
End Sub
Private Sub UnlockPC() Handles xMonitor.Unlock
If xFrmLock Then
frmLock.Close()
xFrmLock = False
nIcon.ShowBalloonTip(500, "Key Found", "PC has been unlocked!", ToolTipIcon.Info)
End If
End Sub
Private Sub StartMonitor()
'some codes here
xMonitor.StartMonitoring()
Me.Refresh()
End Sub
Private Sub StopMonitor()
' some codes here
xMonitor.StopMonitoring()
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
StartMonitor()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
StopMonitor()
End Sub
End Class
或者只是关于这个问题的一个想法:程序在启动时挂起的原因是因为应用程序已加载,而.net框架服务尚未启动。是否可以?
I have created a small multithreaded application using VB.NET. There is no problem when the user manually runs this application. A problem exists when I added this application on startup. It hangs after I rebooted the system. The application is still running its thread but I can't see its GUI because its frozen. If I kill it on task manager and started again, the application works fine. What could be the possible reason/s why this application hangs when added on startup?
Imports System.Threading
Public Class USBLock
Public Event Lock()
Public Event Unlock()
Dim monitorThread As Thread
Public Sub StartMonitoring()
monitorThread = New Thread(AddressOf MonitorNow)
monitorThread.Start()
End Sub
Public Sub StopMonitoring()
Try
monitorThread.Abort()
Catch ex As Exception
End Try
GC.Collect()
End Sub
Private Sub MonitorNow()
'LOOP HERE
If xValid Then
' Enable Block Keyboard
' Hides Taskbar
' Disables Task Manager
RaiseEvent Unlock()
Else
' Disable Block Keyboard
' Shows Taskbar
' Enables Task Manaer
RaiseEvent Lock()
End If
Thread.Sleep(1000)
GC.Collect()
MonitorNow()
End Sub
End Class
Imports System.Reflection
Imports System.IO
Public Class frmMain
Friend WithEvents xMonitor As New USBLock
Dim xCommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Dim xState As Boolean = False
Dim xFrmLock As Boolean
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xFrmLock = False
userEnd = False
If Not (xCommandLineArgs.Contains("-s") Or xCommandLineArgs.Contains("-S")) Then
MyBase.WindowState = FormWindowState.Normal
End If
End Sub
Public Sub New()
MyBase.New()
InitializeComponent()
nIcon.Visible = True
xCommandLineArgs = My.Application.CommandLineArgs
If xCommandLineArgs.Contains("-s") Or xCommandLineArgs.Contains("-S") Then
nIcon.Icon = Drawing.Icon.FromHandle(CType(imgIcon.Images(1), Bitmap).GetHicon())
MyBase.Visible = False
MyBase.WindowState = FormWindowState.Minimized
StartMonitor()
Else
nIcon.Icon = Drawing.Icon.FromHandle(CType(imgIcon.Images(3), Bitmap).GetHicon())
End If
End Sub
Private Sub lockPC() Handles xMonitor.Lock
If Not xFrmLock Then
frmLock.Show()
xFrmLock = True
nIcon.ShowBalloonTip(500, "Key Not Found", "PC has been locked!", ToolTipIcon.Error)
End If
End Sub
Private Sub UnlockPC() Handles xMonitor.Unlock
If xFrmLock Then
frmLock.Close()
xFrmLock = False
nIcon.ShowBalloonTip(500, "Key Found", "PC has been unlocked!", ToolTipIcon.Info)
End If
End Sub
Private Sub StartMonitor()
'some codes here
xMonitor.StartMonitoring()
Me.Refresh()
End Sub
Private Sub StopMonitor()
' some codes here
xMonitor.StopMonitoring()
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
StartMonitor()
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
StopMonitor()
End Sub
End Class
or just a thought on this subject: the reason why the program hangs on startup is because the application is loaded while the .net framework service has not yet started. is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到几个问题。
StopMonitoring
中调用Thread.Abort
根本不是一个好主意。您应该使用更安全的机制来允许线程正常结束。MonitorNow
的递归调用最终将导致StackOverflowException
。StartMonitor
从Form
构造函数中调用。这意味着工作线程有可能在创建消息循环之前启动。USBLock
事件处理程序正在尝试触摸需要首先创建此消息循环的 UI 元素,这一事实使问题变得更加复杂。USBLock
事件句柄来自工作线程,然后尝试触摸 UI 元素。这是绝对不允许的。这样做将导致应用程序意外且严重地失败。尽管如此,很难说为什么应用程序在启动时挂起。我会专注于解决上述问题,然后发布其他问题。
I noticed several issues.
Thread.Abort
inStopMonitoring
is not a good idea at all. You should be using a safer mechanism that allows the thread to gracefully end.GC.Collect
manually. Most of the time this unnecessary and it could make your application perform worse.MonitorNow
will eventually lead to aStackOverflowException
.StartMonitor
is called from theForm
constructor. That means there is a possibility that the worker is started before a message loop has been created. This problem is compounded by the fact that theUSBLock
event handlers are attempting to touch UI elements that require this message loop to be created first.USBLock
event handles are coming in from the worker thread and then attempting to touch UI elements. This is absolutely not allowed. Doing this will result in the application failing unpredictably and spectacularly.Despite all of this it is hard to say why the application hangs when starting up. I would focus on fixing the problems above and then posting other question.