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.
Is this possible? If not then does anyone know the reasoning behind the decision?
Edit: 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:
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();
}
}
Only way I can think of writing a test for this is something like (in rhino mocks)
var monitor = MockRepository.GetMock<IValueMonitor>()
monitor.Expect(x=>x.Start()).Do(new Action(() => {
Thread.Sleep(100);
monitor.Raise(y=>y.ValueAcquired, GetTestValue());
});
but any suggestions are welcomed.