1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . ComponentModel ;
3
4
using System . Windows . Forms ;
4
5
using postgres_database_restore_tool . Helper ;
5
6
using postgres_database_restore_tool . Validator ;
@@ -15,6 +16,20 @@ public PgAdmin()
15
16
AddEventHandlers ( ) ;
16
17
}
17
18
19
+ private void StartLoading ( string msg )
20
+ {
21
+ loadingLbl . Text = msg ;
22
+ loadingLbl . Visible = true ;
23
+ loadingBar . Visible = true ;
24
+ }
25
+
26
+ private void EndLoading ( )
27
+ {
28
+ loadingLbl . Text = "" ;
29
+ loadingLbl . Visible = false ;
30
+ loadingBar . Visible = false ;
31
+ }
32
+
18
33
const string pwdKey = "PGPASSWORD" ;
19
34
20
35
private void AddEventHandlers ( )
@@ -54,8 +69,8 @@ private void OnRestore(object sender, EventArgs e)
54
69
{
55
70
try
56
71
{
72
+ StartLoading ( "Restoring Database" ) ;
57
73
58
-
59
74
var connection = UserConnectionValidator . ValidateConnection ( new UserConnectionVo ( )
60
75
{
61
76
UserName = UserNameElm . Text ,
@@ -67,20 +82,36 @@ private void OnRestore(object sender, EventArgs e)
67
82
} ) ;
68
83
69
84
WorkingStatus . Text = "Restoring..." ;
70
- CommandExecutor . ExecuteRestore ( connection ) ;
71
- WorkingStatus . Text = "Completed" ;
72
- MessageBox . Show ( $ "Database #{ DatabaseElem . Text } restored successfully") ;
73
- SelectedFilelbl . Text = "" ;
74
- WorkingStatus . Text = "" ;
85
+ var bgw = new BackgroundWorker ( ) ;
86
+ bgw . DoWork += ( object _ , DoWorkEventArgs args ) =>
87
+ {
88
+ CommandExecutor . ExecuteRestore ( connection ) ;
89
+ } ;
90
+ bgw . RunWorkerCompleted += ( object _ , RunWorkerCompletedEventArgs args ) =>
91
+ {
92
+ if ( args . Error == null )
93
+ {
94
+ WorkingStatus . Text = "Completed" ;
95
+ MessageBox . Show ( $ "Database #{ DatabaseElem . Text } restored successfully") ;
96
+ }
97
+ else
98
+ {
99
+ var msg = ( args . Error as Exception ) ? . Message ?? "Error during operation" ;
100
+ MessageBox . Show ( msg ) ;
101
+ }
102
+ SelectedFilelbl . Text = "" ;
103
+ WorkingStatus . Text = "" ;
104
+ EndLoading ( ) ;
105
+
106
+ } ;
107
+ bgw . RunWorkerAsync ( ) ;
75
108
}
76
109
catch ( Exception ex )
77
110
{
111
+ EndLoading ( ) ;
78
112
SelectedFilelbl . Text = "" ;
79
113
WorkingStatus . Text = "" ;
80
114
MessageBox . Show ( ex . Message ) ;
81
- }
82
- finally
83
- {
84
115
Environment . SetEnvironmentVariable ( pwdKey , "" ) ;
85
116
}
86
117
}
0 commit comments