0

I have strange problem. I am trying to fast generate data (later, I will use input from I/O) and then draw them.

Because drawing is time consuming, I will add data to other array, to not block generating new ones.

    public MainWindow() {
        InitializeComponent();

        DispatcherTimer adder = new DispatcherTimer {
            Interval = new TimeSpan(0, 0, 0, 0, 1)
        };
        adder.Tick += AddData;
        adder.Start();

        DispatcherTimer refresher = new DispatcherTimer {
            Interval = new TimeSpan(0, 0, 0, 0, 30)
        };
        refresher.Tick += RefreshScreen;
        refresher.Start();
    }

    private void RefreshScreen(object sender, EventArgs e) {
        Debug.WriteLine(queue.Count);
        lock ( queue ) {
            data.InsertRange(0, queue);
            queue.Clear();
        }
        //ToDoLater
    }

    private void AddData(object sender, EventArgs e) {
            queue.Enqueue(new TimedData());
    }

class TimedData {
    public int Value { get; private set; }
    public DateTime Time { get; private set; }

    public TimedData() {
        Time = DateTime.Now;

        Value = new Random().Next(0,200);
    }

I expected queue count around 20-25 but i have only 2-3. That's really low. Where is problem? I tried use List instead of Queue, and to add all items to other list use Queue.Enqueue but with no significant improvement. What am I doing wrong?

110mat110
  • 564
  • 6
  • 26
  • 3
    I would imagine you are running into a timer resolution problem. The standard windows timer has a resolution of 15ms, so scheduling a task every 1ms won't work as you expect. – Dirk Mar 25 '19 at 10:33
  • "drawing is time consuming" is all you need to explain this. The UI thread, like any thread, can do only one thing at a time. It can draw *or* it can respond to a Tick event, it cannot do both. You can use an asynchronous timer and write the code to make it thread-safe, but beware that you can easily create a fire-hose problem where the UI thread can never catch up. Otherwise easy to notice from the UI thread burning 100% core. – Hans Passant Mar 25 '19 at 11:57

0 Answers0