(NET) NET (2023)

Desktop application event handler (ThreadException, UnhandledException, NetworkAvailabilityChanged) and High DPI manifest.

Modern Windows Desktop application has two very important future, (1) Windows event handler and (2) app manifest for HiDPI monitor.

1. Windows event handler.


Processing Unhandled events is main future to multiThreading application and application with Async/Await. Before this button appears in Destop application I use this technology - Trap unhandled exception in windows application. In this technology black console windows appears on short time (less than one second) than Desktop application started.

Current VS2022 has special button, when we click it, new file adding to project.

My typical fast solution to handle this events.


   1:  Imports Microsoft.VisualBasic.ApplicationServices
   2:  Imports Microsoft.VisualBasic.Devices
   3:  Imports System.Reflection
   4:  Imports System.Threading
   5:   
   6:  Namespace My
   7:      ' The following events are available for MyApplication:
   8:      ' Startup: Raised when the application starts, before the startup form is created.
   9:      ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
  10:      ' UnhandledException: Raised if the application encounters an unhandled exception.
  11:      ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
  12:      ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
  13:   
  14:      Partial Friend Class MyApplication
  15:   
  16:              Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
  17:                  My.Application.DoEvents()
  18:              End Sub
  19:   
  20:              Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
  21:                  Dim Data() As Byte = Text.Encoding.UTF8.GetBytes("UnhandledException Error: " & e?.Exception?.Message)
  22:                  My.Computer.FileSystem.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Now.ToBinary.ToString & ".txt"), Data, False)
  23:              End Sub
  24:   
  25:              Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object, e As NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
  26:                  Dim Data() As Byte = Text.Encoding.UTF8.GetBytes("NetworkAvailability Error: ")
  27:                  My.Computer.FileSystem.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Now.ToBinary.ToString & ".txt"), Data, False)
  28:              End Sub
  29:          End Class
  30:   
  31:  End Namespace

In comment we can see possible event, unfortunately we can not see most important event ThreadException. Its not obvious solution how to overcome MS restriction, but possible. There are two solution - with Overloads and with Overrides, both solution workable in this case, but overriding more clear to me.

Basic difference is that - OVERLOADS function are those which are Multiple Function with one name, OVERRIDES fucntion are those which are over written on the functions (if permitted by the writer of that function).


  30:              Public Shared Sub ThreadExceptionEventHandler(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
  31:                  Dim Data() As Byte = Text.Encoding.UTF8.GetBytes("ThreadException Error: " & e?.Exception?.Message)
  32:                  My.Computer.FileSystem.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Now.ToBinary.ToString & ".txt"), Data, False)
  33:              End Sub
  34:   
  35:              Private Overloads Sub OnStartup()
  36:                  AddHandler System.Windows.Forms.Application.ThreadException, AddressOf ThreadExceptionEventHandler
  37:              End Sub

  30:              Public Shared Sub ThreadExceptionEventHandler(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
  31:                  Dim Data() As Byte = Text.Encoding.UTF8.GetBytes("ThreadException Error: " & e?.Exception?.Message)
  32:                  My.Computer.FileSystem.WriteAllBytes(IO.Path.Combine(IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), Now.ToBinary.ToString & ".txt"), Data, False)
  33:              End Sub
  34:   
  35:              Protected Overrides Function OnStartup(e As StartupEventArgs) As Boolean
  36:                  AddHandler Windows.Forms.Application.ThreadException, AddressOf ThreadExceptionEventHandler
  37:                  Return MyBase.OnStartup(e)
  38:              End Function

Usually I testing ThreadException or unhandled exception in this way.



This is example of event handler https://github.com/AAlex-11/RedirectProtocolHandler/blob/main/Handler/ApplicationEvents.vb.

2. App manifest for High DPI monitor

Second main future of modern Desktop development is supporting High DPI monitor resolution.



This is example of workable app.manifest, or look it this https://github.com/AAlex-11/RedirectProtocolHandler/blob/main/Handler/app.manifest.



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: http://www.vb-net.com/AppEventsHandler/Index.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>