@@ -138,7 +138,9 @@ public void SuspendRequested(object sender, RubberduckStatusSuspendParserEventAr
138
138
_parserStateManager . SetStatusAndFireStateChanged ( e . Requestor , ParserState . Busy ,
139
139
CancellationToken . None ) ;
140
140
e . BusyAction . Invoke ( ) ;
141
-
141
+ }
142
+ finally
143
+ {
142
144
lock ( _suspendStackSyncObject )
143
145
{
144
146
_isSuspended = false ;
@@ -148,9 +150,7 @@ public void SuspendRequested(object sender, RubberduckStatusSuspendParserEventAr
148
150
parseRequestor = lastRequestor ;
149
151
}
150
152
}
151
- }
152
- finally
153
- {
153
+
154
154
if ( _parsingSuspendLock . IsWriteLockHeld )
155
155
{
156
156
_parsingSuspendLock . ExitWriteLock ( ) ;
@@ -159,7 +159,6 @@ public void SuspendRequested(object sender, RubberduckStatusSuspendParserEventAr
159
159
160
160
if ( parseRequestor != null )
161
161
{
162
- Cancel ( ) ;
163
162
BeginParse ( parseRequestor , _currentCancellationTokenSource . Token ) ;
164
163
}
165
164
else if ( originalStatus != ParserState . Ready )
@@ -231,11 +230,16 @@ private void ParseInternal(CancellationToken token)
231
230
var lockTaken = false ;
232
231
try
233
232
{
234
- Monitor . Enter ( _parsingRunSyncObject , ref lockTaken ) ;
235
233
if ( ! _parsingSuspendLock . IsWriteLockHeld )
236
234
{
237
235
_parsingSuspendLock . EnterReadLock ( ) ;
238
236
}
237
+ lock ( _cancellationSyncObject )
238
+ {
239
+ Cancel ( ) ;
240
+ token = _currentCancellationTokenSource . Token ;
241
+ }
242
+ Monitor . Enter ( _parsingRunSyncObject , ref lockTaken ) ;
239
243
ParseAllInternal ( this , token ) ;
240
244
}
241
245
catch ( OperationCanceledException )
@@ -244,14 +248,14 @@ private void ParseInternal(CancellationToken token)
244
248
}
245
249
finally
246
250
{
247
- if ( _parsingSuspendLock . IsReadLockHeld )
248
- {
249
- _parsingSuspendLock . ExitReadLock ( ) ;
250
- }
251
251
if ( lockTaken )
252
252
{
253
253
Monitor . Exit ( _parsingRunSyncObject ) ;
254
254
}
255
+ if ( _parsingSuspendLock . IsReadLockHeld )
256
+ {
257
+ _parsingSuspendLock . ExitReadLock ( ) ;
258
+ }
255
259
}
256
260
}
257
261
@@ -404,12 +408,17 @@ private void ParseAll(object requestor, CancellationToken token)
404
408
var lockTaken = false ;
405
409
try
406
410
{
407
- Monitor . Enter ( _parsingRunSyncObject , ref lockTaken ) ;
408
411
if ( ! _parsingSuspendLock . IsWriteLockHeld )
409
412
{
410
413
_parsingSuspendLock . EnterReadLock ( ) ;
411
414
}
412
-
415
+ lock ( _cancellationSyncObject )
416
+ {
417
+ Cancel ( ) ;
418
+ token = _currentCancellationTokenSource . Token ;
419
+ }
420
+ Monitor . Enter ( _parsingRunSyncObject , ref lockTaken ) ;
421
+
413
422
watch = Stopwatch . StartNew ( ) ;
414
423
Logger . Debug ( "Parsing run started. (thread {0})." , Thread . CurrentThread . ManagedThreadId ) ;
415
424
@@ -431,11 +440,11 @@ private void ParseAll(object requestor, CancellationToken token)
431
440
finally
432
441
{
433
442
if ( watch != null && watch . IsRunning ) watch . Stop ( ) ;
443
+ if ( lockTaken ) Monitor . Exit ( _parsingRunSyncObject ) ;
434
444
if ( _parsingSuspendLock . IsReadLockHeld )
435
445
{
436
446
_parsingSuspendLock . ExitReadLock ( ) ;
437
447
}
438
- if ( lockTaken ) Monitor . Exit ( _parsingRunSyncObject ) ;
439
448
}
440
449
if ( watch != null ) Logger . Debug ( "Parsing run finished after {0}s. (thread {1})." , watch . Elapsed . TotalSeconds , Thread . CurrentThread . ManagedThreadId ) ;
441
450
}
0 commit comments