I'm attempting to generate 16 unique DispatcherTimers, without having to recreate the code for each. However, I cannot figure out how to dynamically name them based on the content converted to string of the button pressed.
Originally, I was setting each timer up individually, but this wound up being far too much code to maintain. Now, I've got a method that triggers when one of the 16 buttons is clicked that sets a string to the button's content & passes it to a second method to set up the Dispatcher Timer.
I can't just name the timer in the button click method & pass it, as it tells me it's already used in an enclosing scope to define a local or parameter. I tried naming the timer by concatenating the string "timer" to the end of the variable name, but it didn't like that.
Upon button click
public void StartBtnClicked(object sender, RoutedEventArgs e)
{
string btn = (sender as Button).Content.ToString();
string timerName = btn + "timer";
DispatcherTimerSetup(btn);
}
Setting up the timer
public void DispatcherTimerSetup(string passedBtn)
{
DispatcherTimer passedBtn + "Timer" = new DispatcherTimer();
}
My goal right now is to have the timers be named like "Button1ContentTimer". I'll be using the timers to trigger events upon completion & they will have different TimeSpans. I'll also be implementing a start/stop all button, which is why I'm naming each of them, so I can call them all at once in the start/stop all methods.
Edit:
I'm now creating the timers & adding them to a dictionary. The timers are all named the same, but the string included with them will be different.
public void DispatcherTimerSetup(string btn)
{
Dictionary<string, DispatcherTimer> timerDict =
new Dictionary<string, DispatcherTimer>(); //Set up a dictionary to house all the timers in
DispatcherTimer timer = new DispatcherTimer();
try
{
timerDict.Add(btn, timer);
}
catch (ArgumentException)
{
MessageBox.Show("This timer is already running");
}
}
The StopAll method will receive the dictionary & iterate over it for each timer inside.
static public void StopAll(Dictionary<string, DispatcherTimer> timerDict)
{
foreach(KeyValuePair<string,DispatcherTimer> entry in timerDict)
{
}
}
my only remaining question is how to actually stop these timers? Previously, I would just call timerName.Stop(); multiple times, with a different timer name for each timer.
But now that the timers are all named the same & in a dictionary, I can't figure out how to access them. I tried:
static public void StopAll(Dictionary<string, DispatcherTimer> timerDict)
{
foreach(KeyValuePair<string,DispatcherTimer> entry in timerDict)
{
timerDict.Remove(DispatcherTimer);
}
}
, but it tells me DispatcherTimer is a type which is not valid in the given context. I'm not even sure removing it from the dictionary is the right thing to do, would that stop it? Or do I need to approach this a different way? I feel like there should be a way to actually call each DispatcherTimer element out of the dictionary sequentially, but I haven't been able to figure that one out yet.