/
WindowsFormsApplicationBase.vb
1033 lines (863 loc) · 48.4 KB
/
WindowsFormsApplicationBase.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.
Option Strict On
Option Explicit On
Option Infer On
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.IO.Pipes
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Security
Imports System.Threading
Imports System.Windows.Forms
Imports Microsoft.VisualBasic.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices.Utils
Namespace Microsoft.VisualBasic.ApplicationServices
' Any changes to this enum must be reflected in ValidateAuthenticationModeEnumValue().
Public Enum AuthenticationMode
Windows
ApplicationDefined
End Enum
' Any changes to this enum must be reflected in ValidateShutdownModeEnumValue().
Public Enum ShutdownMode
AfterMainFormCloses
AfterAllFormsClose
End Enum
''' <summary>
''' Signature for the ApplyApplicationDefaults event handler.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub ApplyApplicationDefaultsEventHandler(sender As Object, e As ApplyApplicationDefaultsEventArgs)
''' <summary>
''' Signature for the Startup event handler.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub StartupEventHandler(sender As Object, e As StartupEventArgs)
''' <summary>
''' Signature for the StartupNextInstance event handler.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub StartupNextInstanceEventHandler(sender As Object, e As StartupNextInstanceEventArgs)
''' <summary>
''' Signature for the Shutdown event handler.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub ShutdownEventHandler(sender As Object, e As EventArgs)
''' <summary>
''' Signature for the UnhandledException event handler.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public Delegate Sub UnhandledExceptionEventHandler(sender As Object, e As UnhandledExceptionEventArgs)
''' <summary>
''' Provides the infrastructure for the VB Windows Forms application model.
''' </summary>
''' <remarks>Don't put access on this definition.</remarks>
Partial Public Class WindowsFormsApplicationBase : Inherits ConsoleApplicationBase
''' <summary>
''' Occurs when the application is ready to accept default values for various application areas.
''' </summary>
Public Event ApplyApplicationDefaults As ApplyApplicationDefaultsEventHandler
''' <summary>
''' Occurs when the application starts.
''' </summary>
Public Event Startup As StartupEventHandler
''' <summary>
''' Occurs when attempting to start a single-instance application and the application is already active.
''' </summary>
Public Event StartupNextInstance As StartupNextInstanceEventHandler
''' <summary>
''' Occurs when the application shuts down.
''' </summary>
Public Event Shutdown As ShutdownEventHandler
' Used to marshal a call to Dispose on the Splash Screen.
Private Delegate Sub DisposeDelegate()
' How long a subsequent instance will wait for the original instance to get on its feet.
Private Const SECOND_INSTANCE_TIMEOUT As Integer = 2500 ' milliseconds.
Friend Const MINIMUM_SPLASH_EXPOSURE_DEFAULT As Integer = 2000 ' milliseconds.
Private ReadOnly _splashLock As New Object
Private ReadOnly _appContext As WinFormsAppContext
' Sync object
Private ReadOnly _networkAvailabilityChangeLock As New Object
Private _unhandledExceptionHandlers As List(Of UnhandledExceptionEventHandler)
Private _processingUnhandledExceptionEvent As Boolean
' Tracks whether we need to create the network object so we can listen to the NetworkAvailabilityChanged event.
Private _turnOnNetworkListener As Boolean
' Whether we have made it through the processing of OnInitialize.
Private _finishedOnInitialize As Boolean
Private _networkAvailabilityEventHandlers As List(Of Devices.NetworkAvailableEventHandler)
Private _networkObject As Devices.Network
#Disable Warning IDE0032 ' Use auto property, Justification:=<Public API>
' Whether this app runs using Word like instancing behavior.
Private _isSingleInstance As Boolean
' Defines when the application decides to close.
Private _shutdownStyle As ShutdownMode
' Whether to use Windows XP styles.
Private _enableVisualStyles As Boolean
' We only need to show the splash screen once.
' Protect the user from himself if they are overriding our app model.
Private _didSplashScreen As Boolean
' For splash screens with a minimum display time, this let's us know when that time
' has expired and it is OK to close the splash screen.
Private _splashScreenCompletionSource As TaskCompletionSource(Of Boolean)
Private _formLoadWaiter As AutoResetEvent
Private _splashScreen As Form
' Minimum amount of time to show the splash screen. 0 means hide as soon as the app comes up.
Private _minimumSplashExposure As Integer = MINIMUM_SPLASH_EXPOSURE_DEFAULT
Private _splashTimer As Timers.Timer
Private _appSynchronizationContext As SynchronizationContext
' Informs My.Settings whether to save the settings on exit or not.
Private _saveMySettingsOnExit As Boolean
' The HighDpiMode the user picked from the AppDesigner or assigned to the ApplyHighDpiMode's Event.
Private _highDpiMode As HighDpiMode = HighDpiMode.SystemAware
#Enable Warning IDE0032 ' Use auto property
''' <summary>
''' Occurs when the network availability changes.
''' </summary>
Public Custom Event NetworkAvailabilityChanged As Devices.NetworkAvailableEventHandler
' This is a custom event because we want to hook up the NetworkAvailabilityChanged event only
' if the user writes a handler for it.
' The reason being that it is very expensive to handle and kills our application startup performance.
AddHandler(value As Devices.NetworkAvailableEventHandler)
SyncLock _networkAvailabilityChangeLock
If _networkAvailabilityEventHandlers Is Nothing Then
_networkAvailabilityEventHandlers = New List(Of Devices.NetworkAvailableEventHandler)
End If
_networkAvailabilityEventHandlers.Add(value)
' We don't want to create the network object now - it takes a snapshot
' of the executionContext and our IPrincipal isn't on the thread yet.
' We know we need to create it and we will at the appropriate time
_turnOnNetworkListener = True
' But the user may be doing an AddHandler of their own in which case we need
' to make sure to honor the request. If we aren't past OnInitialize() yet
' we shouldn't do it but the flag above catches that case.
If _networkObject Is Nothing AndAlso _finishedOnInitialize Then
_networkObject = New Devices.Network
Dim windowsFormsApplicationBase As WindowsFormsApplicationBase = Me
AddHandler _networkObject.NetworkAvailabilityChanged,
AddressOf windowsFormsApplicationBase.NetworkAvailableEventAdaptor
End If
End SyncLock
End AddHandler
RemoveHandler(value As Devices.NetworkAvailableEventHandler)
If _networkAvailabilityEventHandlers IsNot Nothing AndAlso
_networkAvailabilityEventHandlers.Count > 0 Then
_networkAvailabilityEventHandlers.Remove(value)
' Last one to leave - turn out the lights...
If _networkAvailabilityEventHandlers.Count = 0 Then
RemoveHandler _networkObject.NetworkAvailabilityChanged, AddressOf NetworkAvailableEventAdaptor
If _networkObject IsNot Nothing Then
' Stop listening to network change events because we are going to go away.
_networkObject.DisconnectListener()
' No sense holding on to this if nobody is listening.
_networkObject = Nothing
End If
End If
End If
End RemoveHandler
RaiseEvent(sender As Object, e As Devices.NetworkAvailableEventArgs)
If _networkAvailabilityEventHandlers IsNot Nothing Then
For Each handler As Devices.NetworkAvailableEventHandler In _networkAvailabilityEventHandlers
Try
If handler IsNot Nothing Then handler.Invoke(sender, e)
Catch ex As Exception
If Not OnUnhandledException(New UnhandledExceptionEventArgs(True, ex)) Then
' The user didn't write a handler so throw the error up the chain.
Throw
End If
End Try
Next
End If
End RaiseEvent
End Event
''' <summary>
''' Occurs when the application encounters an unhandled exception.
''' </summary>
Public Custom Event UnhandledException As UnhandledExceptionEventHandler
' This is a custom event because we want to hook up System.Windows.Forms.Application.ThreadException
' only if the user writes a handler for this event. We only want to hook the ThreadException event
' if the user is handling this event because the act of listening to Application.ThreadException
' causes WinForms to snuff exceptions and we only want WinForms to do that if we are assured that
' the user wrote their own handler to deal with the error instead.
AddHandler(value As UnhandledExceptionEventHandler)
If _unhandledExceptionHandlers Is Nothing Then
_unhandledExceptionHandlers = New List(Of UnhandledExceptionEventHandler)
End If
_unhandledExceptionHandlers.Add(value)
' Only add the listener once so we don't fire the UnHandledException event over and over for the same exception
If _unhandledExceptionHandlers.Count = 1 Then
AddHandler Application.ThreadException, AddressOf OnUnhandledExceptionEventAdaptor
End If
End AddHandler
RemoveHandler(value As UnhandledExceptionEventHandler)
If _unhandledExceptionHandlers IsNot Nothing AndAlso
_unhandledExceptionHandlers.Count > 0 Then
_unhandledExceptionHandlers.Remove(value)
' Last one to leave, turn out the lights...
If _unhandledExceptionHandlers.Count = 0 Then
RemoveHandler Application.ThreadException, AddressOf OnUnhandledExceptionEventAdaptor
End If
End If
End RemoveHandler
RaiseEvent(sender As Object, e As UnhandledExceptionEventArgs)
If _unhandledExceptionHandlers IsNot Nothing Then
' In the case that we throw from the unhandled exception handler, we don't want to
' run the unhandled exception handler again.
_processingUnhandledExceptionEvent = True
For Each handler As UnhandledExceptionEventHandler In _unhandledExceptionHandlers
handler?.Invoke(sender, e)
Next
' Now that we are out of the unhandled exception handler, treat exceptions normally again.
_processingUnhandledExceptionEvent = False
End If
End RaiseEvent
End Event
''' <summary>
''' Constructs the application Shutdown/Startup model object
''' </summary>
''' <remarks>
''' We have to have a parameterless ctor because the platform specific Application object
''' derives from this one and it doesn't define a ctor because the partial class generated by the
''' designer does that to configure the application. </remarks>
Public Sub New()
Me.New(AuthenticationMode.Windows)
End Sub
''' <summary>
''' Constructs the application Shutdown/Startup model object
''' </summary>
<SecuritySafeCritical()>
Public Sub New(authenticationMode As AuthenticationMode)
MyBase.New()
ValidateAuthenticationModeEnumValue(authenticationMode, NameOf(authenticationMode))
' Setup Windows Authentication if that's what the user wanted. Note, we want to do this now,
' before the Network object gets created because the network object will be doing a
' AsyncOperationsManager.CreateOperation() which captures the execution context. So we have
' to have our principal on the thread before that happens.
If authenticationMode = AuthenticationMode.Windows Then
Try
' Consider: Sadly, a call to: System.Security.SecurityManager.IsGranted(New SecurityPermission(SecurityPermissionFlag.ControlPrincipal))
' Will only check THIS caller so you'll always get TRUE.
' What is needed is a way to get to the value of this on a demand basis.
' So I try/catch instead for now but would rather be able to IF my way around this block.
Thread.CurrentPrincipal = New Principal.WindowsPrincipal(Principal.WindowsIdentity.GetCurrent)
Catch ex As SecurityException
End Try
End If
_appContext = New WinFormsAppContext(Me)
' We need to set the WindowsFormsSynchronizationContext because the network object is going to
' get created after this ctor runs (network gets created during event hookup) and we need the
' context in place for it to latch on to. The WindowsFormsSynchronizationContext won't otherwise
' get created until OnCreateMainForm() when the startup form is created and by then it is too late.
' When the startup form gets created, WinForms is going to push our context into the previous context
' and then restore it when Application.Run() exits.
_appSynchronizationContext = AsyncOperationManager.SynchronizationContext
End Sub
''' <summary>
''' Entry point to kick off the VB Startup/Shutdown Application model
''' </summary>
''' <param name="commandLine">The command line from Main()</param>
<SecuritySafeCritical()>
Public Sub Run(commandLine As String())
' Prime the command line args with what we receive from Main() so that Click-Once windows
' apps don't have to do a System.Environment call which would require permissions.
InternalCommandLine = New ReadOnlyCollection(Of String)(commandLine)
If Not IsSingleInstance Then
DoApplicationModel()
Else
' This is a Single-Instance application
' Note: Must pass the calling assembly from here so we can get the running app.
' Otherwise, can break single instance.
Dim ApplicationInstanceID As String = GetApplicationInstanceID(Assembly.GetCallingAssembly)
Dim pipeServer As NamedPipeServerStream = Nothing
If TryCreatePipeServer(ApplicationInstanceID, pipeServer) Then
' --- This is the first instance of a single-instance application to run.
' This is the instance that subsequent instances will attach to.
Using pipeServer
Dim tokenSource = New CancellationTokenSource()
#Disable Warning BC42358 ' Call is not awaited.
WaitForClientConnectionsAsync(pipeServer, AddressOf OnStartupNextInstanceMarshallingAdaptor, cancellationToken:=tokenSource.Token)
#Enable Warning BC42358
DoApplicationModel()
tokenSource.Cancel()
End Using
Else
' --- We are launching a subsequent instance.
Dim tokenSource = New CancellationTokenSource()
tokenSource.CancelAfter(SECOND_INSTANCE_TIMEOUT)
Try
Dim awaitable = SendSecondInstanceArgsAsync(ApplicationInstanceID, commandLine, cancellationToken:=tokenSource.Token).ConfigureAwait(False)
awaitable.GetAwaiter().GetResult()
Catch ex As Exception
Throw New CantStartSingleInstanceException()
End Try
End If
End If 'Single-Instance application
End Sub
''' <summary>
''' Returns the collection of forms that are open. We no longer have thread
''' affinity meaning that this is the WinForms collection that contains Forms that may
''' have been opened on another thread then the one we are calling in on right now.
''' </summary>
Public ReadOnly Property OpenForms() As FormCollection
Get
Return Application.OpenForms
End Get
End Property
''' <summary>
''' Provides access to the main form for this application
''' </summary>
Protected Property MainForm() As Form
Get
Return _appContext?.MainForm
End Get
Set(value As Form)
If value Is Nothing Then
Throw ExceptionUtils.GetArgumentNullException("MainForm", SR.General_PropertyNothing, "MainForm")
End If
If value Is _splashScreen Then
Throw New ArgumentException(GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
End If
_appContext.MainForm = value
End Set
End Property
''' <summary>
''' Provides access to the splash screen for this application
''' </summary>
Public Property SplashScreen() As Form
Get
Return _splashScreen
End Get
Set(value As Form)
' Allow for the case where they set splash screen = nothing and mainForm is currently nothing.
If value IsNot Nothing AndAlso value Is _appContext.MainForm Then
Throw New ArgumentException(GetResourceString(SR.AppModel_SplashAndMainFormTheSame))
End If
_splashScreen = value
End Set
End Property
''' <summary>
''' The splash screen timeout specifies whether there is a minimum time that the splash
''' screen should be displayed for. When not set then the splash screen is hidden
''' as soon as the main form becomes active.
''' </summary>
''' <value>The minimum amount of time, in milliseconds, to display the splash screen.</value>
''' <remarks>
''' This property, although public, used to be set in an `Overrides Function OnInitialize` _before_
''' calling `MyBase.OnInitialize`. We want to phase this out, and with the introduction of the
''' ApplyApplicationDefaults events have it handled in that event, rather than as awkwardly
''' as it is currently suggested to be used in the docs.
''' First step for that is to make it hidden in IntelliSense.
''' </remarks>
<EditorBrowsable(EditorBrowsableState.Never)>
Public Property MinimumSplashScreenDisplayTime() As Integer
Get
Return _minimumSplashExposure
End Get
Set(value As Integer)
_minimumSplashExposure = value
End Set
End Property
''' <summary>
''' Use GDI for the text rendering engine by default.
''' The user can shadow this function to return True if they want their app
''' to use the GDI+ render. We read this function in Main() (My template) to
''' determine how to set the text rendering flag on the WinForms application object.
''' </summary>
''' <returns>True - Use GDI+ renderer. False - use GDI renderer</returns>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Shared ReadOnly Property UseCompatibleTextRendering() As Boolean
Get
Return False
End Get
End Property
''' <summary>
''' Provides the WinForms application context that we are running on
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Public ReadOnly Property ApplicationContext() As ApplicationContext
Get
Return _appContext
End Get
End Property
''' <summary>
''' Informs My.Settings whether to save the settings on exit or not
''' </summary>
Public Property SaveMySettingsOnExit() As Boolean
Get
Return _saveMySettingsOnExit
End Get
Set(value As Boolean)
_saveMySettingsOnExit = value
End Set
End Property
''' <summary>
''' Processes all windows messages currently in the message queue
''' </summary>
Public Sub DoEvents()
Application.DoEvents()
End Sub
''' <summary>
''' This exposes the first in a series of extensibility points for the Startup process. By default, it shows
''' the splash screen and does rudimentary processing of the command line to see if /nosplash or its
''' variants was passed in.
''' </summary>
''' <param name="commandLineArgs"></param>
''' <returns>Returning True indicates that we should continue on with the application Startup sequence</returns>
''' <remarks>This extensibility point is exposed for people who want to override the Startup sequence at the earliest possible point
''' to </remarks>
<EditorBrowsable(EditorBrowsableState.Advanced), STAThread()>
Protected Overridable Function OnInitialize(commandLineArgs As ReadOnlyCollection(Of String)) As Boolean
' Rationale for how we process the default values and how we let the user modify
' them on demand via the ApplyApplicationDefaults event.
' ===========================================================================================
' a) Users used to be able to set MinimumSplashScreenDisplayTime _only_ by overriding OnInitialize
' in a derived class and setting `MyBase.MinimumSplashScreenDisplayTime` there.
' We are picking this (probably) changed value up, and pass it to the ApplyDefaultsEvents
' where it could be modified (again). So event wins over Override over default value (2 seconds).
' b) We feed the default HighDpiMode (SystemAware) to the EventArgs. With the introduction of
' the HighDpiMode property, we give Project System the chance to reflect the HighDpiMode
' in the App Designer UI and have it code-generated based on a modified Application.myapp, which
' would result it to be set in the derived constructor. (See the hidden file in the Solution Explorer
' "My Project\Application.myapp\Application.Designer.vb for how those UI-set values get applied.)
' Once all this is done, we give the User another chance to change the value by code through
' the ApplyDefaults event.
' Overriding MinimumSplashScreenDisplayTime needs still to keep working!
Dim applicationDefaultsEventArgs = New ApplyApplicationDefaultsEventArgs(
MinimumSplashScreenDisplayTime,
HighDpiMode) With
{
.MinimumSplashScreenDisplayTime = MinimumSplashScreenDisplayTime
}
RaiseEvent ApplyApplicationDefaults(Me, applicationDefaultsEventArgs)
If (applicationDefaultsEventArgs.Font IsNot Nothing) Then
Application.SetDefaultFont(applicationDefaultsEventArgs.Font)
End If
MinimumSplashScreenDisplayTime = applicationDefaultsEventArgs.MinimumSplashScreenDisplayTime
' This creates the native window, and that means, we can no longer apply a different Default Font.
' So, this is the earliest point in time to set the AsyncOperationManager's SyncContext.
AsyncOperationManager.SynchronizationContext = New WindowsFormsSynchronizationContext()
_highDpiMode = applicationDefaultsEventArgs.HighDpiMode
' Then, it's applying what we got back as HighDpiMode.
Dim dpiSetResult = Application.SetHighDpiMode(_highDpiMode)
If dpiSetResult Then
_highDpiMode = Application.HighDpiMode
End If
Debug.Assert(dpiSetResult, "We could net set the HighDpiMode.")
' And finally we take care of EnableVisualStyles.
If _enableVisualStyles Then
Application.EnableVisualStyles()
End If
' We'll handle "/nosplash" for you.
If Not (commandLineArgs.Contains("/nosplash") OrElse Me.CommandLineArgs.Contains("-nosplash")) Then
ShowSplashScreen()
End If
_finishedOnInitialize = True
' We are now at a point where we can allow the network object
' to be created since the iPrincipal is on the thread by now.
' True means to not bail out but keep on running after OnInitialize() finishes
Return True
End Function
''' <summary>
''' Extensibility point which raises the Startup event
''' </summary>
''' <param name="eventArgs"></param>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Function OnStartup(eventArgs As StartupEventArgs) As Boolean
eventArgs.Cancel = False
' It is important not to create the network object until the ExecutionContext has everything on it.
' By now the principal will be on the thread so we can create the network object.
' The timing is important because the network object has an AsyncOperationsManager in it that marshals
' the network changed event to the main thread. The asycnOperationsManager does a CreateOperation()
' which makes a copy of the executionContext. That execution context shows up on your thread during
' the callback so I delay creating the network object (and consequently the capturing of the execution context)
' until the principal has been set on the thread. This avoids the problem where My.User isn't set
' during the NetworkAvailabilityChanged event. This problem would just extend itself to any future
' callback that involved the asyncOperationsManager so this is where we need to create objects that
' have a asyncOperationsContext in them.
If _turnOnNetworkListener And _networkObject Is Nothing Then
' The is-nothing-check is to avoid hooking the object more than once.
_networkObject = New Devices.Network
AddHandler _networkObject.NetworkAvailabilityChanged, AddressOf NetworkAvailableEventAdaptor
End If
RaiseEvent Startup(Me, eventArgs)
Return Not eventArgs.Cancel
End Function
''' <summary>
''' Extensibility point which raises the StartupNextInstance
''' </summary>
''' <param name="eventArgs"></param>
<EditorBrowsable(EditorBrowsableState.Advanced)>
<SecuritySafeCritical()>
Protected Overridable Sub OnStartupNextInstance(eventArgs As StartupNextInstanceEventArgs)
RaiseEvent StartupNextInstance(Me, eventArgs)
' Activate the original instance.
If eventArgs.BringToForeground AndAlso MainForm IsNot Nothing Then
If MainForm.WindowState = FormWindowState.Minimized Then
MainForm.WindowState = FormWindowState.Normal
End If
MainForm.Activate()
End If
End Sub
''' <summary>
''' At this point, the command line args should have been processed and the application will create the
''' main form and enter the message loop.
''' </summary>
<SecuritySafeCritical()>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Sub OnRun()
If MainForm Is Nothing Then
' A designer overrides OnCreateMainForm() to set the main form we are supposed to use.
OnCreateMainForm()
If MainForm Is Nothing Then
Throw New NoStartupFormException
End If
' When we have a splash screen that hasn't timed out before the main form is ready to paint, we want to
' block the main form from painting. To do that I let the form get past the Load() event and hold it until
' the splash screen goes down. Then I let the main form continue it's startup sequence. The ordering of
' Form startup events for reference is: Ctor(), Load Event, Layout event, Shown event, Activated event, Paint event.
AddHandler MainForm.Load, AddressOf MainFormLoadingDone
End If
' Run() eats all exceptions (unless running under the debugger). If the user wrote an
' UnhandledException handler we will hook the System.Windows.Forms.Application.ThreadException event
' (see Public Custom Event UnhandledException) which will raise our UnhandledException Event.
' If our user didn't write an UnhandledException event, then we land in the try/catch handler for Forms.Application.Run().
Try
Application.Run(_appContext)
Finally
' When Run() returns, the context we pushed in our ctor (which was a WindowsFormsSynchronizationContext)
' is restored. But we are going to dispose it so we need to disconnect the network listener so that it
' can't fire any events in response to changing network availability conditions through a dead context.
If _networkObject IsNot Nothing Then _networkObject.DisconnectListener()
'Restore the prior sync context.
AsyncOperationManager.SynchronizationContext = _appSynchronizationContext
_appSynchronizationContext = Nothing
End Try
End Sub
''' <summary>
''' A designer will override this method and provide a splash screen if this application has one.
''' </summary>
''' <remarks>For instance, a designer would override this method and emit: Me.Splash = new Splash
''' where Splash was designated in the application designer as being the splash screen for this app</remarks>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Sub OnCreateSplashScreen()
End Sub
''' <summary>
''' Provides a hook that designers will override to set the main form.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Sub OnCreateMainForm()
End Sub
''' <summary>
''' The last in a series of extensibility points for the Shutdown process
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Sub OnShutdown()
RaiseEvent Shutdown(Me, EventArgs.Empty)
End Sub
''' <summary>
''' Raises the UnHandled exception event and exits the application if the event handler indicated
''' that execution shouldn't continue
''' </summary>
''' <param name="e"></param>
''' <returns>True indicates the exception event was raised / False it was not</returns>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Overridable Function OnUnhandledException(e As UnhandledExceptionEventArgs) As Boolean
' Does the user have a handler for this event?
If _unhandledExceptionHandlers IsNot Nothing AndAlso _unhandledExceptionHandlers.Count > 0 Then
' We don't put a try/catch around the handler event so that exceptions in there will
' bubble out - else we will have a recursive exception handler.
RaiseEvent UnhandledException(Me, e)
If e.ExitApplication Then Application.Exit()
' User handled the event.
Return True
End If
' Nobody was listening to the UnhandledException event.
Return False
End Function
''' <summary>
''' Uses the extensibility model to see if there is a splash screen provided for this app and if there is,
''' displays it.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Sub ShowSplashScreen()
If Not _didSplashScreen Then
_didSplashScreen = True
If _splashScreen Is Nothing Then
' If the user specified a splash screen, the designer will have overridden this method to set it.
OnCreateSplashScreen()
End If
If _splashScreen IsNot Nothing Then
' Some splash screens have minimum face time they are supposed to get.
' We'll set up a time to let us know when we can take it down.
If _minimumSplashExposure > 0 Then
' Don't close until the timer expires.
_splashTimer = New Timers.Timer(_minimumSplashExposure)
AddHandler _splashTimer.Elapsed, AddressOf MinimumSplashExposureTimeIsUp
_splashTimer.AutoReset = False
' We only need this, when we actually need to wait for it.
_splashScreenCompletionSource = New TaskCompletionSource(Of Boolean)
' We'll enable it in DisplaySplash() once the splash screen thread gets running.
End If
' Run the splash screen on another thread so we don't starve it for events and painting
' while the main form gets its act together.
Task.Run(AddressOf DisplaySplash)
End If
End If
End Sub
''' <summary>
''' Hide the splash screen. The splash screen was created on another thread
''' thread (main thread) than the one it was run on (secondary thread for the
''' splash screen so it doesn't block app startup. We need to invoke the close.
''' This function gets called from the main thread by the app fx.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Advanced)>
<SecuritySafeCritical()>
Protected Sub HideSplashScreen()
'This ultimately wasn't necessary. I suppose we better keep it for backwards compatibility.
SyncLock _splashLock
' .NET Framework 4.0 (Dev10 #590587) - we now activate the main form before calling
' Dispose on the Splash screen. (we're just swapping the order of the two If blocks.)
' This is to fix the issue where the main form doesn't come to the front after the
' Splash screen disappears.
MainForm?.Activate()
If _splashScreen IsNot Nothing AndAlso Not _splashScreen.IsDisposed Then
Dim disposeSplashDelegate As New DisposeDelegate(AddressOf _splashScreen.Dispose)
_splashScreen.Invoke(disposeSplashDelegate)
_splashScreen = Nothing
End If
End SyncLock
End Sub
''' <summary>
''' Determines when this application will terminate (when the main form goes down, all forms)
''' </summary>
Protected Friend Property ShutdownStyle() As ShutdownMode
Get
Return _shutdownStyle
End Get
Set(value As ShutdownMode)
ValidateShutdownModeEnumValue(value, NameOf(value))
_shutdownStyle = value
End Set
End Property
''' <summary>
''' Determines whether this application will use the XP Windows styles for windows, controls, etc.
''' </summary>
Protected Property EnableVisualStyles() As Boolean
Get
Return _enableVisualStyles
End Get
Set(value As Boolean)
_enableVisualStyles = value
End Set
End Property
''' <summary>
''' Gets or sets the HighDpiMode for the Application.
''' </summary>
<EditorBrowsable(EditorBrowsableState.Never)>
Protected Property HighDpiMode() As HighDpiMode
Get
Return _highDpiMode
End Get
Set(value As HighDpiMode)
_highDpiMode = value
End Set
End Property
<EditorBrowsable(EditorBrowsableState.Advanced)>
Protected Property IsSingleInstance() As Boolean
Get
Return _isSingleInstance
End Get
Set(value As Boolean)
_isSingleInstance = value
End Set
End Property
''' <summary>
''' Validates that the value being passed as an AuthenticationMode enum is a legal value
''' </summary>
''' <param name="value"></param>
Private Shared Sub ValidateAuthenticationModeEnumValue(value As AuthenticationMode, paramName As String)
If value < AuthenticationMode.Windows OrElse value > AuthenticationMode.ApplicationDefined Then
Throw New InvalidEnumArgumentException(paramName, value, GetType(AuthenticationMode))
End If
End Sub
''' <summary>
''' Validates that the value being passed as an ShutdownMode enum is a legal value
''' </summary>
''' <param name="value"></param>
Private Shared Sub ValidateShutdownModeEnumValue(value As ShutdownMode, paramName As String)
If value < ShutdownMode.AfterMainFormCloses OrElse value > ShutdownMode.AfterAllFormsClose Then
Throw New InvalidEnumArgumentException(paramName, value, GetType(ShutdownMode))
End If
End Sub
''' <summary>
''' Displays the splash screen. We get called here from a different thread than what the
''' main form is starting up on. This allows us to process events for the Splash screen so
''' it doesn't freeze up while the main form is getting it together.
''' </summary>
Private Sub DisplaySplash()
Debug.Assert(_splashScreen IsNot Nothing, "We should have never get here if there is no splash screen")
If _splashTimer IsNot Nothing Then
' We only have a timer if there is a minimum time that the splash screen is supposed to be displayed.
' Enable the timer now that we are about to show the splash screen.
_splashTimer.Enabled = True
End If
Application.Run(_splashScreen)
End Sub
''' <summary>
''' If a splash screen has a minimum time out, then once that is up we check to see whether
''' we should close the splash screen. If the main form has activated then we close it.
''' Note that we are getting called on a secondary thread here which isn't necessarily
''' associated with any form. Don't touch forms from this function.
''' </summary>
Private Sub MinimumSplashExposureTimeIsUp(sender As Object, e As Timers.ElapsedEventArgs)
If _splashTimer IsNot Nothing Then
'We only have a timer if there was a minimum timeout on the splash screen.
_splashTimer.Dispose()
_splashTimer = Nothing
End If
_splashScreenCompletionSource.SetResult(True)
End Sub
''' <summary>
''' The Load() event happens before the Shown and Paint events. When we get called here
''' we know that the form load event is done and that the form is about to paint
''' itself for the first time.
''' We can now hide the splash screen.
''' Note that this function gets called from the main thread - the same thread
''' that creates the startup form.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub MainFormLoadingDone(sender As Object, e As EventArgs)
' We don't want this event to call us again.
RemoveHandler MainForm.Load, AddressOf MainFormLoadingDone
' Now, we don't want to eat up a complete Processor Core just for waiting on the main form,
' since this is eating up a LOT of enegery and workload, espacially on Notebooks and tablets.
If _splashScreenCompletionSource IsNot Nothing Then
_formLoadWaiter = New AutoResetEvent(False)
Task.Run(Async Function() As Task
Await _splashScreenCompletionSource.Task.ConfigureAwait(False)
_formLoadWaiter.Set()
End Function)
' Block until the splash screen time is up.
' See MinimumSplashExposureTimeIsUp() which releases us.
_formLoadWaiter.WaitOne()
End If
HideSplashScreen()
End Sub
''' <summary>
''' Handles the Windows.Forms.Application.ThreadException event and raises our Unhandled
''' exception event
''' </summary>
''' <param name="e"></param>
''' <remarks>Our UnHandledException event has a different signature then the Windows.Forms.Application
''' unhandled exception event so we do the translation here before raising our event.
''' </remarks>
Private Sub OnUnhandledExceptionEventAdaptor(sender As Object, e As ThreadExceptionEventArgs)
OnUnhandledException(New UnhandledExceptionEventArgs(True, e.Exception))
End Sub
Private Sub OnStartupNextInstanceMarshallingAdaptor(ByVal args As String())
Dim invoked = False
Try
Dim handleNextInstance As New Action(
Sub()
invoked = True
OnStartupNextInstance(New StartupNextInstanceEventArgs(
New ReadOnlyCollection(Of String)(args),
bringToForegroundFlag:=True))
End Sub)
' If we have a Main form, we need to make sure that we are on _its_ UI thread
' before we call OnStartupNextInstance.
If MainForm IsNot Nothing Then
MainForm.Invoke(handleNextInstance)
Else
' Otherwise, we need to make sure that we are on the thread
' provided by the SynchronizationContext.
AsyncOperationManager.
SynchronizationContext.
Send(Sub() handleNextInstance(), Nothing)
End If
Catch ex As Exception When Not invoked
' Only catch exceptions thrown when the UI thread is not available, before
' the UI thread has been created or after it has been terminated. Exceptions
' thrown from OnStartupNextInstance() should be allowed to propagate.
End Try
End Sub
''' <summary>
''' Handles the Network.NetworkAvailability event (on the correct thread) and raises the
''' NetworkAvailabilityChanged event
''' </summary>
''' <param name="Sender">Contains the Network instance that raised the event</param>
''' <param name="e">Contains whether the network is available or not</param>
Private Sub NetworkAvailableEventAdaptor(sender As Object, e As Devices.NetworkAvailableEventArgs)
RaiseEvent NetworkAvailabilityChanged(sender, e)
End Sub
''' <summary>
''' Runs the user's program through the VB Startup/Shutdown application model
''' </summary>
Private Sub DoApplicationModel()
Dim EventArgs As New StartupEventArgs(CommandLineArgs)
' Only do the try/catch if we aren't running under the debugger.
' If we do try/catch under the debugger the debugger never gets
' a crack at exceptions which breaks the exception helper.
If Not Debugger.IsAttached Then
' NO DEBUGGER ATTACHED - we use a catch so that we can run our UnhandledException code
' Note - Sadly, code changes within this IF (that don't pertain to exception handling)
' need to be mirrored in the ELSE debugger attached clause below.
Try
If OnInitialize(CommandLineArgs) Then
If OnStartup(EventArgs) Then
OnRun()
OnShutdown()
End If
End If
Catch ex As Exception
' This catch is for exceptions that happen during the On* methods above,
' but have occurred outside of the message pump (which exceptions we would
' have already seen via our hook of System.Windows.Forms.Application.ThreadException).
If _processingUnhandledExceptionEvent Then
' If the UnhandledException handler threw for some reason, throw that error out to the system.
Throw
Else
' We had an exception, but not during the OnUnhandledException handler so give the user
' a chance to look at what happened in the UnhandledException event handler
If Not OnUnhandledException(New UnhandledExceptionEventArgs(True, ex)) Then
' The user didn't write a handler so throw the error out to the system
Throw
End If
End If
End Try
Else
' DEBUGGER ATTACHED - we don't have an uber catch when debugging so the exception
' will bubble out to the exception helper.
' We also don't hook up the Application.ThreadException event because WinForms ignores it
' when we are running under the debugger.
If OnInitialize(CommandLineArgs) Then
If OnStartup(EventArgs) Then
OnRun()