-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWaitingForm.cs
114 lines (99 loc) · 3.46 KB
/
WaitingForm.cs
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
using GHLCP.Diagnostics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GHLCP
{
public partial class WaitingForm : Form
{
private readonly Gamedir gamedir;
private readonly string[] filenames;
public WaitingForm(Gamedir gamedir, string[] filenames)
{
InitializeComponent();
this.gamedir = gamedir;
this.filenames = filenames;
importProgressBar.Maximum = filenames.Length;
}
private void WaitingForm_Load(object sender, EventArgs e)
{
UpdateStatusLabel();
importBackgroundWorker.RunWorkerAsync();
}
private void importBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
gamedir.ImportProgressChanged += gamedir_ImportProgressChanged;
foreach (string file in filenames)
{
if (importBackgroundWorker.CancellationPending)
{
e.Cancel = true;
break;
}
gamedir.HandleImport(file);
importBackgroundWorker.ReportProgress(1);
}
}
private void importBackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (e.UserState is ImportProgressChangedEventArgs progress)
{
progressLabel.Text = progress.Message;
} else
{
importProgressBar.Value += e.ProgressPercentage;
UpdateStatusLabel();
}
}
private void importBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
gamedir.ImportProgressChanged -= gamedir_ImportProgressChanged;
if (e.Cancelled)
{
statusLabel.Text = "Track import canceled";
statusLabel.ForeColor = Color.Red;
} else if (e.Error != null)
{
statusLabel.Text = e.Error is InvalidOperationException ? e.Error.Message : "An unknown error occurred while importing " + Path.GetFileName(filenames[importProgressBar.Value]);
statusLabel.ForeColor = Color.Red;
Logger.Instance.Error(e.Error.Message);
} else
{
statusLabel.Text = "Track import completed";
statusLabel.ForeColor = Color.Green;
}
progressLabel.Text = "";
cancelButton.Enabled = true;
cancelButton.Text = "Close";
}
private void gamedir_ImportProgressChanged(object sender, ImportProgressChangedEventArgs e)
{
importBackgroundWorker.ReportProgress(0, e);
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (importBackgroundWorker.IsBusy)
{
importBackgroundWorker.CancelAsync();
} else
{
Close();
}
cancelButton.Enabled = false;
}
private void UpdateStatusLabel()
{
if (importProgressBar.Value < filenames.Length)
{
statusLabel.Text = $"Importing : {Path.GetFileName(filenames[importProgressBar.Value])}";
}
}
}
}