From 9b39f0b4ae3bfa64b98bd2d331abda6486be9291 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 19 Dec 2023 08:36:37 +0100 Subject: [PATCH] fix init --- EnvelopeGenerator.Form/frmSplashScreen.vb | 76 ++++++++++++----------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/EnvelopeGenerator.Form/frmSplashScreen.vb b/EnvelopeGenerator.Form/frmSplashScreen.vb index e761ce6e..e9f945c1 100644 --- a/EnvelopeGenerator.Form/frmSplashScreen.vb +++ b/EnvelopeGenerator.Form/frmSplashScreen.vb @@ -11,7 +11,6 @@ Imports System.ComponentModel Public Class frmSplashScreen Private LogConfig As LogConfig Private Logger As Logger - Private Database As MSSQLServer Private ConfigManager As ConfigManager(Of Config) Private WithEvents Worker As New BackgroundWorker() With {.WorkerReportsProgress = True} @@ -22,25 +21,54 @@ Public Class frmSplashScreen Me.BringToFront() End Sub - Private Sub Worker_DoWork(sender As Object, e As DoWorkEventArgs) Handles Worker.DoWork + Private Sub frmSplashScreen_Shown(sender As Object, e As EventArgs) Handles Me.Shown + Dim oLogPath = IO.Path.Combine(Application.LocalUserAppDataPath, "Log") + LogConfig = New LogConfig(LogConfig.PathType.CustomPath, oLogPath, CompanyName:="Digital Data", ProductName:="Envelope Generator") + Logger = LogConfig.GetLogger() + + Try + ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath, Application.StartupPath) + + If ConfigManager.Config.ConnectionString = String.Empty Then + Dim oSQLConfig As New frmSQLConfig(LogConfig) + If oSQLConfig.ShowDialog() = DialogResult.OK Then + ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString + ConfigManager.Save() + + MsgBox("Database configured. Application will restart.", MsgBoxStyle.Critical, Text) + Application.Restart() + Else + MsgBox("No Database configured. Application will close!", MsgBoxStyle.Critical, Text) + Application.Exit() + End If + End If + + Worker.RunWorkerAsync(New State() With { + .User = Nothing, + .UserId = 0, + .LogConfig = LogConfig, + .Config = ConfigManager.Config, + .Database = Nothing, + .DbConfig = New DbConfig() + }) + Catch ex As Exception + Logger.Error(ex) + MsgBox("Fehler beim Initialisieren: " & ex.Message & vbNewLine & vbNewLine & ex.StackTrace, MsgBoxStyle.Critical, Text) + Application.Exit() + + End Try + End Sub + Private Sub Worker_DoWork(sender As Object, e As DoWorkEventArgs) Handles Worker.DoWork Dim oState As State = DirectCast(e.Argument, State) Worker.ReportProgress(20, "Initialize Database") Thread.Sleep(300) Dim oConnectionString = MSSQLServer.DecryptConnectionString(oState.Config.ConnectionString) - Database = New MSSQLServer(oState.LogConfig, oConnectionString) - - oState = New State With { - .UserId = 0, - .Config = oState.Config, - .DbConfig = New DbConfig(), - .LogConfig = LogConfig, - .Database = Database - } + oState.Database = New MSSQLServer(oState.LogConfig, oConnectionString) - If Database.DBInitialized = False Then + If oState.Database?.DBInitialized = False Then Throw New ApplicationException("Could not connect to the database. Application will close!") End If @@ -108,31 +136,7 @@ Public Class frmSplashScreen Close() End Sub - Private Sub frmSplashScreen_Shown(sender As Object, e As EventArgs) Handles Me.Shown - Dim oLogPath = IO.Path.Combine(Application.LocalUserAppDataPath, "Log") - LogConfig = New LogConfig(LogConfig.PathType.CustomPath, oLogPath, CompanyName:="Digital Data", ProductName:="Envelope Generator") - Logger = LogConfig.GetLogger() - ConfigManager = New ConfigManager(Of Config)(LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath, Application.StartupPath) - - If ConfigManager.Config.ConnectionString = String.Empty Then - Dim oSQLConfig As New frmSQLConfig(LogConfig) - If oSQLConfig.ShowDialog() = DialogResult.OK Then - ConfigManager.Config.ConnectionString = oSQLConfig.ConnectionString - ConfigManager.Save() - - MsgBox("Database configured. Application will restart.", MsgBoxStyle.Critical, Text) - Application.Restart() - Else - MsgBox("No Database configured. Application will close!", MsgBoxStyle.Critical, Text) - Application.Exit() - End If - End If - Worker.RunWorkerAsync(New State() With { - .LogConfig = LogConfig, - .Config = ConfigManager.Config - }) - End Sub End Class \ No newline at end of file