我正在创建一个BCI界面,当我使用 System.Windows.Timer 时,按钮必须在特定频率闪烁,但这不是非常精确,闪烁不是指定的频率。我用来制作闪烁按钮的类是:
StateWithColor[] colors = new StateWithColor[] {
new StateWithColor(0, Color.Black),
new StateWithColor(1, Color.White)
};
public class FlickTimer<T> : IDisposable
where T : Control
{
public T Target { get; set; }
protected readonly IList<StateWithColor> possibleStates = new List<StateWithColor>();
protected int currentState = 0;
//protected int currentState_2 = 1;
protected object lockState = new object();
protected Timer timer = new Timer();
protected void Flicker(object sender, EventArgs e)
{
if (Target == null)
{
return;
}
if (Target.InvokeRequired)
{
Target.Invoke(new EventHandler(Flicker), sender, e);
return;
}
lock (lockState)
{
Target.BackColor = possibleStates[currentState].Color;
//int color = currentState + 1;
//Target.ForeColor = possibleStates[currentState_2].Color;
currentState++;
//currentState_2--;
if (currentState >= possibleStates.Count)
{
currentState = 0;
//currentState_2 = 1;
}
}
}
public FlickTimer(StateWithColor[] states, int timeout = 0, T target = null)
{
Target = target;
lock (lockState)
{
foreach (var state in states)
{
possibleStates.Add(state);
}
}
timer.Interval = timeout;
timer.Tick += Flicker;
Start();
}
public void Start()
{
timer.Start();
}
public void Stop()
{
timer.Stop();
}
public void Dispose()
{
if (timer != null)
{
Stop();
timer.Tick -= Flicker;
timer = null;
}
}
}
public struct StateWithColor
{ public int State;
public Color Color;
public StateWithColor(int state, Color color)
{
Color = color;
State = state;
}
}
这不会给出运行接口时指定的频率。用于界面的屏幕设置为120HZ,界面包含9个按钮,其频率应为以下频率:6,6.5,7,7.5,8,9,10,11,12.5。或9个频率,跨度为6-15HZ。频率可以是6.546或6.5并不重要。
有没有办法做到这一点,并获得精确的频率?