forked from DanWBR/dwsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationEvents.vb
211 lines (156 loc) · 8.92 KB
/
ApplicationEvents.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
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System.Linq
Imports DWSIM.Interfaces
Namespace My
' The following events are availble for MyApplication:
'
' Startup: Raised when the application starts, before the startup form is created.
' Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
' UnhandledException: Raised if the application encounters an unhandled exception.
' StartupNextInstance: Raised when launching a single-instance application and the application is already active.
' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
<DllImport("kernel32.dll", SetLastError:=True)> Private Shared Function AddDllDirectory(lpPathName As String) As Boolean
End Function
Public _ResourceManager As System.Resources.ResourceManager
Public _HelpManager As System.Resources.ResourceManager
Public _PropertyNameManager As System.Resources.ResourceManager
Public _CultureInfo As System.Globalization.CultureInfo
Public Property CalculatorStopRequested As Boolean = False
Public Property MasterCalculatorStopRequested As Boolean = False
Public Property CommandLineMode As Boolean = False
Public Property MainThreadId As Integer
Public Property CalculatorBusy As Boolean = False
Public UtilityPlugins As New Dictionary(Of String, Interfaces.IUtilityPlugin)
Public Property PushUndoRedoAction As Boolean = True
Public Property ActiveSimulation As FormFlowsheet
Public Property MainWindowForm As FormMain
Public Property UserUnitSystems As Dictionary(Of String, SystemsOfUnits.Units)
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If Environment.OSVersion.Version >= New Version(6, 3, 0) Then
'win 8.1 added support for per monitor dpi
If (Environment.OSVersion.Version >= New Version(10, 0, 15063)) Then
'creators update added support For per monitor v2
NativeMethods.SetProcessDpiAwarenessContext(NativeMethods.DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
Else
NativeMethods.SetProcessDpiAwareness(NativeMethods.PROCESS_DPI_AWARENESS.Process_Per_Monitor_DPI_Aware)
End If
Else
NativeMethods.SetProcessDPIAware()
End If
Directory.SetCurrentDirectory(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location))
Control.CheckForIllegalCrossThreadCalls = True
'upgrade settings from previous build, if applicable.
If My.Settings.UpgradeRequired Then
My.Settings.Upgrade()
My.Settings.UpgradeRequired = False
End If
'check if the user wants to reset settings.
If My.Computer.Keyboard.ShiftKeyDown Then
My.Settings.Reset()
'MessageBox.Show("The settings were reset successfully.")
End If
'loads the current language
If My.Settings.CultureInfo = "de" Or My.Settings.CultureInfo = "es" Or My.Settings.CultureInfo = "en-US" Then
My.Settings.CultureInfo = "en"
End If
_CultureInfo = New Globalization.CultureInfo(My.Settings.CultureInfo)
My.Application.ChangeUICulture(My.Settings.CultureInfo)
'loads the resource manager
_ResourceManager = New System.Resources.ResourceManager("DWSIM.DWSIM", System.Reflection.Assembly.GetExecutingAssembly())
'loads the help manager
_HelpManager = New System.Resources.ResourceManager("DWSIM.Tips", System.Reflection.Assembly.GetExecutingAssembly())
'loads the property name manager
_PropertyNameManager = New System.Resources.ResourceManager("DWSIM.Properties", System.Reflection.Assembly.GetExecutingAssembly())
For Each s As String In My.Application.CommandLineArgs
If s.ToLower = "-commandline" Then
My.Application.CommandLineMode = True
e.Cancel = True
End If
If s.ToLower = "-locale" Then
Dim clcult As String = My.Application.CommandLineArgs(My.Application.CommandLineArgs.IndexOf(s) + 1)
_CultureInfo = New Globalization.CultureInfo(clcult)
My.Application.ChangeUICulture(clcult)
End If
Next
DWSIM.App.InitializeSettings()
'If My.Settings.PythonPath <> "" Then
' AddDllDirectory(My.Settings.PythonPath)
'Else
' My.Settings.PythonPath = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "python\python-3.6.8.amd64\")
' If Directory.Exists(My.Settings.PythonPath) Then
' GlobalSettings.Settings.PythonPath = My.Settings.PythonPath
' AddDllDirectory(My.Settings.PythonPath)
' End If
'End If
If e.Cancel Then
'command line processor
AttachConsole(ATTACH_PARENT_PROCESS)
Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush = True
Console.SetOut(standardOutput)
Dim f1 As New FormMain
End If
End Sub
Private Sub MyApplication_Shutdown(sender As Object, e As EventArgs) Handles Me.Shutdown
'save user unit systems
Dim xdoc As New XDocument()
Dim xel As XElement
xdoc = New XDocument
xdoc.Add(New XElement("Units"))
For Each su2 As SystemsOfUnits.Units In UserUnitSystems.Values
xdoc.Element("Units").Add(New XElement(XmlConvert.EncodeName(su2.Name)))
xel = xdoc.Element("Units").Element(XmlConvert.EncodeName(su2.Name))
xel.Add(su2.SaveData())
Next
Using sw As New StringWriter()
Using xw As New XmlTextWriter(sw)
xdoc.Save(xw)
My.Settings.UserUnits = sw.ToString
End Using
End Using
If Not DWSIM.App.IsRunningOnMono Then
My.Settings.Save()
End If
End Sub
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
Logging.Logger.LogUnhandled("Unhandled Exception", e.Exception)
If Not CommandLineMode Then
If Not GlobalSettings.Settings.AutomationMode Then
Dim frmEx As New FormUnhandledException
frmEx.ex = e.Exception
frmEx.ShowDialog()
e.ExitApplication = False
If FormMain.AnalyticsProvider IsNot Nothing Then
FormMain.AnalyticsProvider.RegisterError("Unhandled Exception", "", e.Exception, Nothing)
End If
End If
Else
If Not GlobalSettings.Settings.AutomationMode Then
Console.Write(e.Exception.ToString)
e.ExitApplication = True
End If
End If
End Sub
Private Function GetSplashScreen() As Form
Dim splfile = Path.Combine(Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().Location), "extenders", "SplashScreen.dll")
If File.Exists(splfile) Then
Dim types = Assembly.LoadFrom(splfile).GetExportedTypes()
Dim tList As List(Of Type) = types.ToList().FindAll(Function(t) t.GetInterfaces().Contains(GetType(ISplashScreen)))
Dim lst = tList.ConvertAll(Function(t As Type) TryCast(Activator.CreateInstance(t), ISplashScreen))
Return lst(0).GetSplashScreen()
Else
Return Global.DWSIM.SplashScreen
End If
End Function
<System.Runtime.InteropServices.DllImport("kernel32.dll")>
Private Shared Function AttachConsole(dwProcessId As Integer) As Boolean
End Function
Private Const ATTACH_PARENT_PROCESS As Integer = -1
<System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError:=True)>
Friend Shared Function FreeConsole() As Integer
End Function
End Class
End Namespace