Skip to content

Commit 4138ba7

Browse files
committed
Add overload of Poller.Run to allow user to specify SynchronizationContext
1 parent f634094 commit 4138ba7

File tree

1 file changed

+68
-28
lines changed

1 file changed

+68
-28
lines changed

src/NetMQ/NetMQPoller.cs

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ public void RunAsync(string threadName)
355355
m_switch.WaitForOn();
356356
}
357357

358+
#if NET35
358359
/// <summary>
359360
/// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
360361
/// </summary>
@@ -364,16 +365,68 @@ public void Run()
364365
if (IsRunning)
365366
throw new InvalidOperationException("NetMQPoller is already running");
366367

367-
#if NET35
368368
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+
}
369382
#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+
370404
var oldSynchronisationContext = SynchronizationContext.Current;
371-
SynchronizationContext.SetSynchronizationContext(new NetMQSynchronizationContext(this));
405+
SynchronizationContext.SetSynchronizationContext(syncContext);
372406
m_isSchedulerThread.Value = true;
373-
#endif
374-
m_stopSignaler.Reset();
375407

408+
m_stopSignaler.Reset();
376409
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+
{
377430
try
378431
{
379432
// Recalculate all timers now
@@ -487,21 +540,8 @@ public void Run()
487540
}
488541
finally
489542
{
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);
505545
}
506546
}
507547

@@ -540,7 +580,7 @@ public void StopAsync()
540580
m_stopSignaler.RequestStop();
541581
}
542582

543-
#endregion
583+
#endregion
544584

545585
private void OnSocketEventsChanged(object sender, NetMQSocketEventArgs e)
546586
{
@@ -579,7 +619,7 @@ private void RebuildPollset()
579619
m_isPollSetDirty = false;
580620
}
581621

582-
#region IEnumerable
622+
#region IEnumerable
583623

584624
/// <summary>This class only implements <see cref="IEnumerable"/> in order to support collection initialiser syntax.</summary>
585625
/// <returns>An empty enumerator.</returns>
@@ -588,9 +628,9 @@ IEnumerator IEnumerable.GetEnumerator()
588628
yield break;
589629
}
590630

591-
#endregion
631+
#endregion
592632

593-
#region IDisposable
633+
#region IDisposable
594634

595635
private enum DisposeState
596636
{
@@ -647,9 +687,9 @@ public void Dispose()
647687
m_disposeState = (int)DisposeState.Disposed;
648688
}
649689

650-
#endregion
690+
#endregion
651691

652-
#region ISynchronizeInvoke
692+
#region ISynchronizeInvoke
653693

654694
#if NET40
655695
IAsyncResult ISynchronizeInvoke.BeginInvoke(Delegate method, object[] args)
@@ -678,9 +718,9 @@ object ISynchronizeInvoke.Invoke(Delegate method, object[] args)
678718
bool ISynchronizeInvoke.InvokeRequired => !CanExecuteTaskInline;
679719
#endif
680720

681-
#endregion
721+
#endregion
682722

683-
#region Synchronisation context
723+
#region Synchronisation context
684724

685725
#if !NET35
686726
private sealed class NetMQSynchronizationContext : SynchronizationContext
@@ -709,6 +749,6 @@ public override void Send(SendOrPostCallback d, object state)
709749
}
710750
#endif
711751

712-
#endregion
752+
#endregion
713753
}
714754
}

0 commit comments

Comments
 (0)