11using System ;
22using System . Drawing ;
3+ using System . ComponentModel ;
34using System . Windows . Forms ;
45
56namespace CrossStitchProject
@@ -28,7 +29,39 @@ private void PreviewButton_Click(object sender, EventArgs e)
2829 {
2930 if ( string . IsNullOrEmpty ( openFileDialog1 . FileName ) ) return ;
3031 previewButton . Enabled = false ;
32+ ShowWaitCursor ( ) ;
33+ if ( ! previewWorker . IsBusy )
34+ {
35+ previewWorker . RunWorkerAsync ( ) ;
36+ }
37+ }
38+
39+ private void CrossStitchButton_Click ( object sender , EventArgs e )
40+ {
41+ crossStitchButton . Enabled = false ;
42+ ShowWaitCursor ( ) ;
43+ if ( ! previewWorker . IsBusy )
44+ {
45+ patternWorker . RunWorkerAsync ( ) ;
46+ }
47+ }
48+ private static void ShowWaitCursor ( )
49+ {
50+ Application . UseWaitCursor = true ;
3151 Cursor . Current = Cursors . WaitCursor ;
52+ }
53+ private static void ShowNormalCursor ( )
54+ {
55+ Application . UseWaitCursor = false ;
56+ Cursor . Current = Cursors . Default ;
57+ }
58+ private static void DisplayException ( string message , Exception e )
59+ {
60+ MessageBox . Show ( e . ToString ( ) , message , MessageBoxButtons . OK ) ;
61+ }
62+
63+ private void PreviewWorker_DoWork ( object sender , System . ComponentModel . DoWorkEventArgs e )
64+ {
3265 var filename = openFileDialog1 . FileName ;
3366 var colorPattern = ColorCheckBox . Checked ;
3467 var ditherImage = ditherCB . Checked ;
@@ -40,6 +73,25 @@ private void PreviewButton_Click(object sender, EventArgs e)
4073 var crossStitcher = new CrossStitcher
4174 ( b , colorPattern , ditherImage , projectName ) ;
4275 var preview = crossStitcher . GenerateStitchBitmap ( ) ;
76+ e . Result = preview ;
77+ }
78+ catch ( Exception ex )
79+ {
80+ e . Result = ex ;
81+ previewWorker . CancelAsync ( ) ;
82+ }
83+ }
84+ private void PreviewWorker_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e )
85+ {
86+ if ( e . Cancelled )
87+ {
88+ DisplayException ( "Error generating preview" , ( Exception ) e . Result ) ;
89+ }
90+ else
91+ {
92+ previewButton . Enabled = true ;
93+ ShowNormalCursor ( ) ;
94+ var preview = ( Bitmap ) e . Result ;
4395 using ( var previewForm = new Form ( ) )
4496 {
4597 previewForm . StartPosition = FormStartPosition . CenterScreen ;
@@ -54,22 +106,10 @@ private void PreviewButton_Click(object sender, EventArgs e)
54106 previewForm . ShowDialog ( ) ;
55107 }
56108 }
57- catch ( Exception ex )
58- {
59- DisplayException ( "Error generating preview" , ex ) ;
60- }
61- finally
62- {
63- previewButton . Enabled = true ;
64- Cursor . Current = Cursors . Default ;
65- }
66-
67109 }
68110
69- private void CrossStitchButton_Click ( object sender , EventArgs e )
111+ private void PatternWorker_DoWork ( object sender , DoWorkEventArgs e )
70112 {
71- crossStitchButton . Enabled = false ;
72- Cursor . Current = Cursors . WaitCursor ;
73113 var filename = openFileDialog1 . FileName ;
74114 var colorPattern = ColorCheckBox . Checked ;
75115 var ditherImage = ditherCB . Checked ;
@@ -84,18 +124,21 @@ private void CrossStitchButton_Click(object sender, EventArgs e)
84124 }
85125 catch ( Exception ex )
86126 {
87- DisplayException ( "Error occurred during generation" , ex ) ;
127+ e . Result = ex ;
128+ patternWorker . CancelAsync ( ) ;
129+ }
130+ }
131+ private void PatternWorker_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e )
132+ {
133+ if ( e . Cancelled )
134+ {
135+ DisplayException ( "Error generating preview" , ( Exception ) e . Result ) ;
88136 }
89- finally
137+ else
90138 {
91139 crossStitchButton . Enabled = true ;
92- Cursor . Current = Cursors . Default ;
140+ ShowNormalCursor ( ) ;
93141 }
94142 }
95-
96- private static void DisplayException ( string message , Exception e )
97- {
98- MessageBox . Show ( e . ToString ( ) , message , MessageBoxButtons . OK ) ;
99- }
100143 }
101144}
0 commit comments