0

I have an old complex VB.Net Windows application that runs on .Net Framework 4.8. It runs just fine if I keep interacting with it. But if I leave it idle for 2-3 hours, it stops working. I looked in task manager and even tried clicking on the program itself and it does not say "not responding" and looks like it is responding and it is not frozen but nothing is not working. By the way, the main screen is just a couple of flags that the user clicks and selects the language. When I click on the flags, nothing happens. I have a log for almost every action that happens in the application. But I can not find any log at all after I left the application idle. This means nothing happened and the application just stopped responding to events. Looks like it decided to detach all the events.

I looked in Task Manager and it is not using any CPU (it bounces between 0.5% to 3%), and Task Manager says it is working just fine and not showing "not responding" in front of the application. I even tried to pause the application using Visual Studio to see if the program is stuck in any loop or not, but VS shows nothing and it says your application is in "External Code". There is nothing odd in the diagnosis of Visual Studio or anywhere else.

Abbas Dehghan
  • 381
  • 4
  • 12
  • Are you able to debug it with the original code? – djv Jun 13 '23 at 13:29
  • Yes, I am. It also happens in debugging. There is a film playing in the background. I can hear the film's sound stop when the application is freezing. – Abbas Dehghan Jun 13 '23 at 13:41
  • Then you can't debug the code anymore at that point? – djv Jun 13 '23 at 13:47
  • Yes. I can not debug anymore because it is not responding to any input. I think my situation is very close to this thread. https://stackoverflow.com/questions/11585703/application-runform-is-hanging I have not tried to put a breakpoint on the paint method yet to see if it is running or not. – Abbas Dehghan Jun 13 '23 at 13:52
  • The [accepted answer](https://stackoverflow.com/a/11635127/832052) is a good place to look in your case too. Do you do anything with multi-threading? – djv Jun 13 '23 at 13:55
  • Is this a server, laptop, desktop? What does the application do? Is it interacting with any USB devices? Have you checked the Windows power management settings? – Tu deschizi eu inchid Jun 13 '23 at 15:01
  • It is a checking application running on a desktop system with a touchscreen monitor connected to a ID scanner for passengers to scan their ID card and a printer to print their ticket – Abbas Dehghan Jun 14 '23 at 08:05
  • @djv Yes. There are lots of Invoke calls – Abbas Dehghan Jun 14 '23 at 08:06
  • @AbbasDehghan you mean Control.Invoke, right? I would look at if InvokeRequired / Invoke is being used appropriately everywhere it should be – djv Jun 14 '23 at 19:39

1 Answers1

0

For the people who may have the same problem that I had:

My problem was the same as the problem described in this thread. I followed this answer to find what is causing the problem. I had to convert their code into VB:

#Region "Handle out of main thread creation!!!"
   Private WithEvents FreezeTimer As New Windows.Forms.Timer
   Private Sub StartFreezeHandling()
      FreezeTimer = New Windows.Forms.Timer With {
         .Interval = 60000
      }
      FreezeTimer.Start()
   End Sub
   Private Sub StopFreezeHandling()
      FreezeTimer.Stop()
   End Sub

   Private Sub CheckSystemEventsHandlersForFreeze() Handles FreezeTimer.Tick
      Dim handlers = GetType(SystemEvents).GetField("_handlers", BindingFlags.NonPublic Or BindingFlags.[Static]).GetValue(Nothing)
      Dim handlersValues = handlers.[GetType]().GetProperty("Values").GetValue(handlers)

      For Each invokeInfos In (TryCast(handlersValues, IEnumerable)).OfType(Of Object)().ToArray()

         For Each invokeInfo In (TryCast(invokeInfos, IEnumerable)).OfType(Of Object)().ToArray()
            Dim syncContext = invokeInfo.[GetType]().GetField("_syncContext", BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(invokeInfo)
            If syncContext Is Nothing Then Throw New Exception("syncContext missing")
            If Not (TypeOf syncContext Is WindowsFormsSynchronizationContext) Then Continue For
            Dim threadRef = CType(syncContext.[GetType]().GetField("destinationThreadRef", BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(syncContext), WeakReference)
            If Not threadRef.IsAlive Then Continue For
            Dim thread = CType(threadRef.Target, Thread)
            If thread.ManagedThreadId = 1 Then Continue For
            Dim dlg = CType(invokeInfo.[GetType]().GetField("_delegate", BindingFlags.NonPublic Or BindingFlags.Instance).GetValue(invokeInfo), [Delegate])
            logSvc.Logging(New List(Of String)() From {$"SystemEvents handler '{dlg.Method.DeclaringType}.{dlg.Method.Name}' with name" +
                           " '{DirectCast(dlg.Target, Control).Name}' could freeze app due to wrong thread: " &
                           $"{thread.ManagedThreadId},{thread.IsThreadPoolThread},{thread.IsAlive},{thread.Name}"})
         Next
      Next
   End Sub
#End Region

My problem was trying to scan the ID card outside the main thread. So I changed the call this way:

    scnDoc = Me.Invoke(Function()
                          Return boDesko.ScanPassport(oBook, CurrentScan, CurrLanguage)
                       End Function)
Abbas Dehghan
  • 381
  • 4
  • 12