Interligado.CompareExchange com enum
Estou tentando usarInterlocked.CompareExchange
com este enum:
public enum State {
Idle,
Running,
//...
}
O código a seguir não compila, mas é o que eu quero fazer:
if (Interlocked.CompareExchange(ref state, State.Running, State.Idle) != State.Idle) {
throw new InvalidOperationException("Unable to run - not idle");
}
Claro que posso usar um int em vez do enum e usar uma propriedade:
private int state = (int)State.Idle;
public State { get { return (State)state; } }
Então, elenco as enums para um int:
if (Interlocked.CompareExchange(ref state, (int)State.Running, (int)State.Idle) != (int)State.Idle) {
throw new InvalidOperationException("Unable to run - not idle");
}
Mas existem maneiras melhores de fazer isso?