@@ -16,7 +16,7 @@ namespace Toastify.Threading
16
16
17
17
public T Window { get ; private set ; }
18
18
19
- public Thread Thread { get ; }
19
+ public Thread Thread { get ; private set ; }
20
20
21
21
public bool IsBackground
22
22
{
@@ -65,6 +65,7 @@ public void Start()
65
65
public void Abort ( )
66
66
{
67
67
ThreadManager . Instance . Abort ( this . Thread ) ;
68
+ this . Thread = null ;
68
69
}
69
70
70
71
public void Join ( )
@@ -101,25 +102,41 @@ public void CloseWindow()
101
102
}
102
103
catch ( Exception ex )
103
104
{
104
- logger . Error ( $ "Unhandled error while closing { nameof ( WindowThread < T > ) } 's window{ ( ! string . IsNullOrWhiteSpace ( this . Window ? . Title ) ? $ " ({ this . Window . Title } )" : string . Empty ) } ", ex ) ;
105
+ if ( this . Window != null )
106
+ {
107
+ this . Window . Dispatcher . Invoke ( DispatcherPriority . Normal , new ThreadStart ( ( ) =>
108
+ {
109
+ string title = ! string . IsNullOrWhiteSpace ( this . Window ? . Title ) ? $ " ({ this . Window . Title } )" : string . Empty ;
110
+ logger . Error ( $ "Unhandled error while closing { nameof ( WindowThread < T > ) } 's window{ title } ", ex ) ;
111
+ } ) ) ;
112
+ }
113
+ else
114
+ logger . Error ( $ "Unhandled error while closing { nameof ( WindowThread < T > ) } 's window", ex ) ;
105
115
}
106
116
}
107
117
108
118
private void ThreadStart ( )
109
119
{
120
+ var syncContext = SynchronizationContext . Current ;
121
+ SynchronizationContext . SetSynchronizationContext ( new DispatcherSynchronizationContext ( Dispatcher . CurrentDispatcher ) ) ;
122
+
110
123
try
111
124
{
112
- var syncContext = SynchronizationContext . Current ;
113
- SynchronizationContext . SetSynchronizationContext ( new DispatcherSynchronizationContext ( Dispatcher . CurrentDispatcher ) ) ;
114
-
115
125
this . Window = new T ( ) ;
116
126
this . options ? . WindowInitialization ? . Invoke ( this . Window ) ;
117
127
118
128
this . Window . Closed += ( sender , args ) =>
119
129
{
120
- Application . Current ? . Dispatcher ? . BeginInvoke ( new Action ( ( ) => Application . Current . Exit -= this . Application_Exit ) ) ;
121
- this . options ? . OnWindowClosingAction ? . Invoke ( this ) ;
122
- this . Window ? . Dispatcher ? . BeginInvokeShutdown ( DispatcherPriority . Background ) ;
130
+ try
131
+ {
132
+ Application . Current ? . Dispatcher ? . BeginInvoke ( new Action ( ( ) => Application . Current . Exit -= this . Application_Exit ) ) ;
133
+ this . options ? . OnWindowClosingAction ? . Invoke ( this ) ;
134
+ this . Window ? . Dispatcher ? . BeginInvokeShutdown ( DispatcherPriority . Background ) ;
135
+ }
136
+ catch
137
+ {
138
+ // ignore
139
+ }
123
140
} ;
124
141
Application . Current ? . Dispatcher ? . Invoke ( ( ) => Application . Current . Exit += this . Application_Exit ) ;
125
142
@@ -128,7 +145,6 @@ private void ThreadStart()
128
145
this . options ? . AfterWindowShownAction ? . Invoke ( this . Window ) ;
129
146
130
147
Dispatcher . Run ( ) ;
131
- SynchronizationContext . SetSynchronizationContext ( syncContext ) ;
132
148
}
133
149
catch ( ThreadAbortException )
134
150
{
@@ -141,11 +157,13 @@ private void ThreadStart()
141
157
{
142
158
this . CloseWindow ( ) ;
143
159
Dispatcher . CurrentDispatcher . BeginInvokeShutdown ( DispatcherPriority . Background ) ;
160
+ SynchronizationContext . SetSynchronizationContext ( syncContext ) ;
144
161
}
145
162
}
146
163
147
164
public override int GetHashCode ( )
148
165
{
166
+ // ReSharper disable once NonReadonlyMemberInGetHashCode
149
167
return this . Thread ? . GetHashCode ( ) ?? 0 ;
150
168
}
151
169
0 commit comments