Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to enforce an atomic operation with no context switch in c#?
    primarykey
    data
    text
    <p>I have several lines of a method that I would like to ensure that there is no context switch to another thread while executing these? Yes, re-architecting is an option but it would be far more expedient for now if I could do this.</p> <p>Is this possible? If not then does anyone know the reasoning behind the decision?</p> <p><strong>Edit:</strong> The reason I am asking is that I have a class responsible for returning a value, the value is provided via an event so when GetValue() is called, the thread needs to block until the event is raised. So we have:</p> <pre><code>public class ValueResolver { IPersistentNotifier _notifier; IValueMonitor _monitor; Value _value; ManualResetEvent _resolvedEvent = new ManualResetEvent(false); public ValueResolver(IPersistentNotifier notifier, IValueMonitor monitor) { _notifier = notifier; _monitor = monitor; _monitor.ValueAcquired += ValueAcquired; } public Value GetValue() { _value = null; persistentNotifier.Show("Getting Value") _monitor.Start(); _resolvedEvent.WaitOne(60000, false); return _value } public void ValueAcquired(Value val) { _value = val; _monitor.Stop(); _notifier.Hide(); _resolvedEvent.Set(); } } </code></pre> <p>Only way I can think of writing a test for this is something like (in rhino mocks)</p> <pre><code>var monitor = MockRepository.GetMock&lt;IValueMonitor&gt;() monitor.Expect(x=&gt;x.Start()).Do(new Action(() =&gt; { Thread.Sleep(100); monitor.Raise(y=&gt;y.ValueAcquired, GetTestValue()); }); </code></pre> <p>but any suggestions are welcomed.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload