Skip to content

Commit 52f59d9

Browse files
authored
Merge pull request #700 from simonholt/custom_synccontext
Allow user to specify custom sync context
2 parents e98aa4a + 576feb4 commit 52f59d9

File tree

1 file changed

+59
-19
lines changed

1 file changed

+59
-19
lines changed

src/NetMQ/NetMQPoller.cs

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ public void RunAsync(string threadName)
359359
m_switch.WaitForOn();
360360
}
361361

362+
#if NET35
362363
/// <summary>
363364
/// Runs the poller on the caller's thread. Only returns when <see cref="Stop"/> or <see cref="StopAsync"/> are called from another thread.
364365
/// </summary>
@@ -368,16 +369,68 @@ public void Run()
368369
if (IsRunning)
369370
throw new InvalidOperationException("NetMQPoller is already running");
370371

371-
#if NET35
372372
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+
}
373386
#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+
374408
var oldSynchronisationContext = SynchronizationContext.Current;
375-
SynchronizationContext.SetSynchronizationContext(new NetMQSynchronizationContext(this));
409+
SynchronizationContext.SetSynchronizationContext(syncContext);
376410
m_isSchedulerThread.Value = true;
377-
#endif
378-
m_stopSignaler.Reset();
379411

412+
m_stopSignaler.Reset();
380413
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+
{
381434
try
382435
{
383436
// Recalculate all timers now
@@ -497,21 +550,8 @@ public void Run()
497550
}
498551
finally
499552
{
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);
515555
}
516556
}
517557

0 commit comments

Comments
 (0)