0

I'm trying to send/publish at 100ms, and the message looks like this x.x.x.x.x.x.x.x.x.x So every 100ms or so subscribe will be called. My problem is that I think, it's not fast enough, (i.e if the current subscribe is not yet done and another subscribe is being called/message being published)

I was thinking, on how could I keep on populating the list, at the same time graph with Oxyplot. Can I use threading for this?

var x = 0;
channel.Subscribe(message =>
            {
                this.RunOnUiThread(() =>
                {

                    var sample =  message.Data;
                    byte[] data = (byte[])sample;
                    var data1 = System.Text.Encoding.ASCII.GetString(data);
                    var splitData = data1.Split('-');
                    foreach(string s in splitData) //Contains 10
                    {
                        double y = double.Parse(s);
                        y /= 100;
                        series1.Points.Add(new DataPoint(x, y));
                        MyModel.InvalidatePlot(true);
                        x++;
                    }
                    if (x >= xaxis.Maximum)
                    {
                        xaxis.Pan(xaxis.Transform(-1 + xaxis.Offset));
                    }


                });
            });

1 Answers1

0

Guaranteeing a minimum execution time goes into Realtime Programming. And with a App on a Smartphone OS you are about as far from that I can imagine you to be. The only thing farther "off" would be any interpreted langauge (PHP, Python).

The only thing you can do is define a minimum time between itterations. I did once wrote some example code for doing that from within a alternative thread. A basic rate limiting code:

integer interval = 20;
DateTime dueTime = DateTime.Now.AddMillisconds(interval);

while(true){
  if(DateTime.Now >= dueTime){
    //insert code here

    //Update next dueTime
    dueTime = DateTime.Now.AddMillisconds(interval);
  }
  else{
    //Just yield to not tax out the CPU
    Thread.Sleep(1);
  }
}

Note that DateTime.Now only resturns something new about every 18 ms, so anything less then 20 would be too little.

If you think you can not afford a minimum time, you may need to read the Speed Rant.

Christopher
  • 9,634
  • 2
  • 17
  • 31