I am building a device emulator. When it starts, it takes some time for it to initialized. This would be logically represented by being turned on and going immediately to an "Initialization" state, and after some time it goes to "Ready" state.
I am using MVVM, so the ViewModel will for now represent all the device logic. Each of the possible states have a datatriggered style to be rendered by the View. If I just set the state when I build the viewmodel, the view renders with the correct appearance.
What I want to do is to create a "timeout state", that is, when some event occurs (starting the application, clicking a certain button), the device enters a state for a fixed time, and then falls back to the "ready", or "idle" state.
I thought about using Sleep, but sleep blocks the UI (so they say). So I think about using Threads, but I am not sure how to do it. This is what I have got so far:
using System.ComponentModel;
namespace EmuladorMiotool {
public class MiotoolViewModel : INotifyPropertyChanged {
Estados _estado;
public Estados Estado {
get {
return _estado;
}
set {
_estado = value;
switch (_estado) {
case Estados.WirelessProcurando:
// WAIT FOR TWO SECONDS WITHOUT BLOCKING GUI
// It should look like the device is actually doing something
// (but not indeed, for now)
_estado = Estados.WirelessConectado;
break;
}
RaisePropertyChanged("Estado");
}
}
public MiotoolViewModel() {
// The constructor sets the initial state to "Searching"
Estado = Estados.WirelessProcurando;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public enum Estados {
UsbOcioso,
UsbAquisitando,
UsbTransferindo,
WirelessN?oPareado,
WirelessPareado,
WirelessDesconectado,
WirelessProcurando,
WirelessConectado,
WirelessAquisitando,
DataLoggerOcioso,
DataLoggerAquisitando,
Erro,
Formatando
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…