.Net 应用程序在启动时添加时挂起

发布于 2024-11-05 07:45:05 字数 3175 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-11-12 07:45:05

我注意到几个问题。

  1. StopMonitoring 中调用 Thread.Abort 根本不是一个好主意。您应该使用更安全的机制来允许线程正常结束。
  2. 我认为手动调用 GC.Collect 没有意义。大多数时候这是不必要的,它可能会使您的应用程序性能更差。
  3. MonitorNow 的递归调用最终将导致 StackOverflowException
  4. StartMonitorForm 构造函数中调用。这意味着工作线程有可能在创建消息循环之前启动。 USBLock 事件处理程序正在尝试触摸需要首先创建此消息循环的 UI 元素,这一事实使问题变得更加复杂。
  5. USBLock 事件句柄来自工作线程,然后尝试触摸 UI 元素。这是绝对不允许的。这样做将导致应用程序意外且严重地失败。

尽管如此,很难说为什么应用程序在启动时挂起。我会专注于解决上述问题,然后发布其他问题。

I noticed several issues.

  1. Calling Thread.Abort in StopMonitoring is not a good idea at all. You should be using a safer mechanism that allows the thread to gracefully end.
  2. I see no point in calling GC.Collect manually. Most of the time this unnecessary and it could make your application perform worse.
  3. The recursive call to MonitorNow will eventually lead to a StackOverflowException.
  4. StartMonitor is called from the Form 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 the USBLock event handlers are attempting to touch UI elements that require this message loop to be created first.
  5. The 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文