11using System ;
22using System . Collections . Generic ;
3+ using System . ComponentModel ;
34using System . Windows . Forms ;
45using postgres_database_restore_tool . Helper ;
56using postgres_database_restore_tool . Validator ;
@@ -15,6 +16,20 @@ public PgAdmin()
1516 AddEventHandlers ( ) ;
1617 }
1718
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+
1833 const string pwdKey = "PGPASSWORD" ;
1934
2035 private void AddEventHandlers ( )
@@ -54,8 +69,8 @@ private void OnRestore(object sender, EventArgs e)
5469 {
5570 try
5671 {
72+ StartLoading ( "Restoring Database" ) ;
5773
58-
5974 var connection = UserConnectionValidator . ValidateConnection ( new UserConnectionVo ( )
6075 {
6176 UserName = UserNameElm . Text ,
@@ -67,20 +82,36 @@ private void OnRestore(object sender, EventArgs e)
6782 } ) ;
6883
6984 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 ( ) ;
75108 }
76109 catch ( Exception ex )
77110 {
111+ EndLoading ( ) ;
78112 SelectedFilelbl . Text = "" ;
79113 WorkingStatus . Text = "" ;
80114 MessageBox . Show ( ex . Message ) ;
81- }
82- finally
83- {
84115 Environment . SetEnvironmentVariable ( pwdKey , "" ) ;
85116 }
86117 }
0 commit comments