开发者

.Net Application hangs when added on startup

开发者 https://www.devze.com 2023-03-02 18:26 出处:网络
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 aft

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 xCo开发者_JAVA技巧mmandLineArgs.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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消