@@ -355,6 +355,7 @@ public void RunAsync(string threadName)
355
355
m_switch . WaitForOn ( ) ;
356
356
}
357
357
358
+ #if NET35
358
359
/// <summary>
359
360
/// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
360
361
/// </summary>
@@ -364,16 +365,68 @@ public void Run()
364
365
if ( IsRunning )
365
366
throw new InvalidOperationException ( "NetMQPoller is already running" ) ;
366
367
367
- #if NET35
368
368
m_pollerThread = Thread . CurrentThread ;
369
+ m_stopSignaler . Reset ( ) ;
370
+ m_switch . SwitchOn ( ) ;
371
+
372
+ try
373
+ {
374
+ RunPoller ( ) ;
375
+ }
376
+ finally
377
+ {
378
+ m_pollerThread = null ;
379
+ m_switch . SwitchOff ( ) ;
380
+ }
381
+ }
369
382
#else
383
+ /// <summary>
384
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
385
+ /// </summary>
386
+ public void Run ( )
387
+ {
388
+ Run ( new NetMQSynchronizationContext ( this ) ) ;
389
+ }
390
+
391
+ /// <summary>
392
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop" /> or <see cref="StopAsync" /> are called from another thread.
393
+ /// </summary>
394
+ /// <param name="syncContext">The synchronization context that will be used.</param>
395
+ public void Run ( SynchronizationContext syncContext )
396
+ {
397
+ if ( syncContext == null )
398
+ throw new ArgumentNullException ( "Must supply a Synchronization Context" ) ;
399
+
400
+ CheckDisposed ( ) ;
401
+ if ( IsRunning )
402
+ throw new InvalidOperationException ( "NetMQPoller is already running" ) ;
403
+
370
404
var oldSynchronisationContext = SynchronizationContext . Current ;
371
- SynchronizationContext . SetSynchronizationContext ( new NetMQSynchronizationContext ( this ) ) ;
405
+ SynchronizationContext . SetSynchronizationContext ( syncContext ) ;
372
406
m_isSchedulerThread . Value = true ;
373
- #endif
374
- m_stopSignaler . Reset ( ) ;
375
407
408
+ m_stopSignaler . Reset ( ) ;
376
409
m_switch . SwitchOn ( ) ;
410
+
411
+ try
412
+ {
413
+ RunPoller ( ) ;
414
+ }
415
+ finally
416
+ {
417
+ m_isSchedulerThread . Value = false ;
418
+ SynchronizationContext . SetSynchronizationContext ( oldSynchronisationContext ) ;
419
+ m_switch . SwitchOff ( ) ;
420
+ }
421
+
422
+ }
423
+ #endif
424
+
425
+ /// <summary>
426
+ /// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
427
+ /// </summary>
428
+ private void RunPoller ( )
429
+ {
377
430
try
378
431
{
379
432
// Recalculate all timers now
@@ -487,21 +540,8 @@ public void Run()
487
540
}
488
541
finally
489
542
{
490
- try
491
- {
492
- foreach ( var socket in m_sockets . ToList ( ) )
493
- Remove ( socket ) ;
494
- }
495
- finally
496
- {
497
- #if NET35
498
- m_pollerThread = null ;
499
- #else
500
- m_isSchedulerThread . Value = false ;
501
- SynchronizationContext . SetSynchronizationContext ( oldSynchronisationContext ) ;
502
- #endif
503
- m_switch . SwitchOff ( ) ;
504
- }
543
+ foreach ( var socket in m_sockets . ToList ( ) )
544
+ Remove ( socket ) ;
505
545
}
506
546
}
507
547
@@ -540,7 +580,7 @@ public void StopAsync()
540
580
m_stopSignaler . RequestStop ( ) ;
541
581
}
542
582
543
- #endregion
583
+ #endregion
544
584
545
585
private void OnSocketEventsChanged ( object sender , NetMQSocketEventArgs e )
546
586
{
@@ -579,7 +619,7 @@ private void RebuildPollset()
579
619
m_isPollSetDirty = false ;
580
620
}
581
621
582
- #region IEnumerable
622
+ #region IEnumerable
583
623
584
624
/// <summary>This class only implements <see cref="IEnumerable"/> in order to support collection initialiser syntax.</summary>
585
625
/// <returns>An empty enumerator.</returns>
@@ -588,9 +628,9 @@ IEnumerator IEnumerable.GetEnumerator()
588
628
yield break ;
589
629
}
590
630
591
- #endregion
631
+ #endregion
592
632
593
- #region IDisposable
633
+ #region IDisposable
594
634
595
635
private enum DisposeState
596
636
{
@@ -647,9 +687,9 @@ public void Dispose()
647
687
m_disposeState = ( int ) DisposeState . Disposed ;
648
688
}
649
689
650
- #endregion
690
+ #endregion
651
691
652
- #region ISynchronizeInvoke
692
+ #region ISynchronizeInvoke
653
693
654
694
#if NET40
655
695
IAsyncResult ISynchronizeInvoke . BeginInvoke ( Delegate method , object [ ] args )
@@ -678,9 +718,9 @@ object ISynchronizeInvoke.Invoke(Delegate method, object[] args)
678
718
bool ISynchronizeInvoke . InvokeRequired => ! CanExecuteTaskInline ;
679
719
#endif
680
720
681
- #endregion
721
+ #endregion
682
722
683
- #region Synchronisation context
723
+ #region Synchronisation context
684
724
685
725
#if ! NET35
686
726
private sealed class NetMQSynchronizationContext : SynchronizationContext
@@ -709,6 +749,6 @@ public override void Send(SendOrPostCallback d, object state)
709
749
}
710
750
#endif
711
751
712
- #endregion
752
+ #endregion
713
753
}
714
754
}
0 commit comments