-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplashForm.cs
More file actions
68 lines (62 loc) · 2.35 KB
/
SplashForm.cs
File metadata and controls
68 lines (62 loc) · 2.35 KB
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
public void StartInitialization(Action<Action<string>> initWithReporter)
{
var bw = new BackgroundWorker { WorkerReportsProgress = true };
bw.DoWork += (s, e) =>
{
// Reporter an den Initialisierer weitergeben
initWithReporter(msg => bw.ReportProgress(0, msg));
};
bw.ProgressChanged += (s, e) =>
{
if (!this.IsHandleCreated) return;
// RunWorkerCallbacks laufen auf UI-Thread; hier direkt setzen ist okay
this.lblStatus.Text = e.UserState as string ?? string.Empty;
};
bw.RunWorkerCompleted += (s, e) =>
{
try
{
// Wichtig: erst auf e.Error prüfen (Ausnahme im DoWork)
if (e.Error != null)
{
// Loggen für Diagnose
System.Diagnostics.Debug.WriteLine("Initialization error: " + e.Error);
if (this.IsHandleCreated)
{
this.lblStatus.Text = "Initialization failed";
MessageBox.Show(this, "Initialization error: " + e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
return;
}
// Falls der Worker abgebrochen wurde
if (e.Cancelled)
{
this.lblStatus.Text = "Initialization cancelled";
this.Close();
return;
}
// Falls DoWork ein Exception-Objekt als Result zurückgegeben hat (falls Du das behalten willst)
if (e.Result is Exception ex)
{
System.Diagnostics.Debug.WriteLine("Initialization returned exception in Result: " + ex);
if (this.IsHandleCreated)
{
this.lblStatus.Text = "Initialization failed";
MessageBox.Show(this, "Initialization error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.Close();
return;
}
// Normal beendet
this.Close();
}
catch (Exception ex)
{
// Falls trotzdem eine Exception hier auftaucht, logge sie
System.Diagnostics.Debug.WriteLine("RunWorkerCompleted handler crashed: " + ex);
try { this.Close(); } catch { }
}
};
bw.RunWorkerAsync();
}