1

Basically I have to make a Dispatcher timer, store the variable in the array as it counts then display the history array in a text box when I click the rawDataButton. For some reason my array only shows the most recent ones and never stores in the array, here's my code

DispatcherTimer timer = new DispatcherTimer();
MeasureLengthDevice dg = new MeasureLengthDevice();
int mostRecnet = 0;
int[] arr = new int[10];

private void StartCollectingDataButton_Click(object sender, RoutedEventArgs e)
{
    timer.Interval = new TimeSpan(0, 0, 1);
    timer.Start();
    timer.Tick += timer_Tick;
}
private void timer_Tick(object sender, object e)
{
    mostRecnet = dg.GetMeasurement();
    recentDataTextBlock.Text = mostRecnet.ToString();
    for(int i = 0; i < arr.Length; i++)
    {
        arr[i] = mostRecnet;
    }
}
private void stopCollectingDataButton_Click(object sender, RoutedEventArgs e)
{
    timer.Stop();
}

private void rawDataButton_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < arr.Length; i++)
    {
        mostRecnet = arr[i];
        rawDataTextBox.Text += arr[i].ToString() + "\n";
    }
}
preciousbetine
  • 2,959
  • 3
  • 13
  • 29
  • 1
    It looks like you're using a single measurement to fill all elements of the array. – ChiefTwoPencils Oct 26 '19 at 20:35
  • You propably wanted to have `List`, rather then an array. No need to manually organize the Array. – Christopher Oct 26 '19 at 21:13
  • Of course, with a Dispatcher timer Concurrency might be an issue. And a concurrent list does not exist. Of coruse as you only ever need to read the List afterwards, this might not be a big issue. – Christopher Oct 26 '19 at 21:14
  • @ChiefTwoPencils yeah I noticed that too after debugging, but I'm trying to store the mostRecent inside the array as it goes. – CapitalistNick Oct 26 '19 at 21:18
  • Like another said, a list would be better. – ChiefTwoPencils Oct 26 '19 at 21:24
  • Yeah, List works way better, thank you but if I wanted to do the same thing with an Array how could you do that, I'm curious now and want to learn. Thanks to all again – CapitalistNick Oct 26 '19 at 21:48
  • As a note, you are attaching the Tick handler each time the start button is clicked, but never remove it later. With each click, you are thus increasing the number of times the handler is called. Move `timer.Tick += timer_Tick;` out of the Click handler, into a constructor or initialize method. And *of course*, with a DispatcherTimer concurency is not at all an issue, because all your code runs in the UI thread. You can safely ignore that comment and the answer. – Clemens Oct 27 '19 at 18:47

1 Answers1

1

You can replace

int mostRecnet = 0;
int[] arr = new int[10];

And the need to manually count with a list and some basic mutex code:

//Always use a dedicated object to lock onto
private object mutex = new object();
var History = new List<int>();

In the tick you do this:

lock(mutex){
  History.Add(mostRecent);
}

And when reading it out, you do this:

lock(mutex){
  //do whatever you need to output the list contents
}

Do be warned that locking for output will prevent the tick event from processing. And especially bulk writes to the UI can take considerable amount of time, easily in the 10's-100's of Milliseconds.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • 1
    Using `lock` with a DispatcherTimer is terrible nonsense. The Tick handler already runs in the UI thread, and there is no concurrency issue at all. – Clemens Oct 29 '19 at 23:20