@@ -359,6 +359,7 @@ public void RunAsync(string threadName)
359
359
m_switch . WaitForOn ( ) ;
360
360
}
361
361
362
+ #if NET35
362
363
/// <summary>
363
364
/// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
364
365
/// </summary>
@@ -368,16 +369,68 @@ public void Run()
368
369
if ( IsRunning )
369
370
throw new InvalidOperationException ( "NetMQPoller is already running" ) ;
370
371
371
- #if NET35
372
372
m_pollerThread = Thread . CurrentThread ;
373
+ m_stopSignaler . Reset ( ) ;
374
+ m_switch . SwitchOn ( ) ;
375
+
376
+ try
377
+ {
378
+ RunPoller ( ) ;
379
+ }
380
+ finally
381
+ {
382
+ m_pollerThread = null ;
383
+ m_switch . SwitchOff ( ) ;
384
+ }
385
+ }
373
386
#else
387
+ /// <summary>
388
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
389
+ /// </summary>
390
+ public void Run ( )
391
+ {
392
+ Run ( new NetMQSynchronizationContext ( this ) ) ;
393
+ }
394
+
395
+ /// <summary>
396
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop" /> or <see cref="StopAsync" /> are called from another thread.
397
+ /// </summary>
398
+ /// <param name="syncContext">The synchronization context that will be used.</param>
399
+ public void Run ( SynchronizationContext syncContext )
400
+ {
401
+ if ( syncContext == null )
402
+ throw new ArgumentNullException ( "Must supply a Synchronization Context" ) ;
403
+
404
+ CheckDisposed ( ) ;
405
+ if ( IsRunning )
406
+ throw new InvalidOperationException ( "NetMQPoller is already running" ) ;
407
+
374
408
var oldSynchronisationContext = SynchronizationContext . Current ;
375
- SynchronizationContext . SetSynchronizationContext ( new NetMQSynchronizationContext ( this ) ) ;
409
+ SynchronizationContext . SetSynchronizationContext ( syncContext ) ;
376
410
m_isSchedulerThread . Value = true ;
377
- #endif
378
- m_stopSignaler . Reset ( ) ;
379
411
412
+ m_stopSignaler . Reset ( ) ;
380
413
m_switch . SwitchOn ( ) ;
414
+
415
+ try
416
+ {
417
+ RunPoller ( ) ;
418
+ }
419
+ finally
420
+ {
421
+ m_isSchedulerThread . Value = false ;
422
+ SynchronizationContext . SetSynchronizationContext ( oldSynchronisationContext ) ;
423
+ m_switch . SwitchOff ( ) ;
424
+ }
425
+
426
+ }
427
+ #endif
428
+
429
+ /// <summary>
430
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
431
+ /// </summary>
432
+ private void RunPoller ( )
433
+ {
381
434
try
382
435
{
383
436
// Recalculate all timers now
@@ -497,21 +550,8 @@ public void Run()
497
550
}
498
551
finally
499
552
{
500
- try
501
- {
502
- foreach ( var socket in m_sockets . ToList ( ) )
503
- Remove ( socket ) ;
504
- }
505
- finally
506
- {
507
- #if NET35
508
- m_pollerThread = null ;
509
- #else
510
- m_isSchedulerThread . Value = false ;
511
- SynchronizationContext . SetSynchronizationContext ( oldSynchronisationContext ) ;
512
- #endif
513
- m_switch . SwitchOff ( ) ;
514
- }
553
+ foreach ( var socket in m_sockets . ToList ( ) )
554
+ Remove ( socket ) ;
515
555
}
516
556
}
517
557
0 commit comments