Closed EricZeiberg closed 1 year ago
I am not able to reproduce this, it probably means that the concurrency issue could be on your side, please could you provide a repository with the issue?
You can test the library by cloning this repo and go to the multithreading sample, then to create a similar case to yours just modify the ReadData
method to:
private async void ReadData()
{
await Task.Delay(1000);
while (true)
{
await Task.Delay(_delay);
for (var i = 0; i < 2000; i++)
{
_current = Interlocked.Add(ref _current, _r.Next(-9, 10));
lock (Sync)
{
_values.Add(_current);
_values.RemoveAt(0);
}
await Task.Delay(1);
}
}
}
Finally set these 2 variables in the constructor of the VM:
_delay = 1000;
var readTasks = 5;
With those settings we are adding/removing about 10,000 points per seconds I event can use tooltips without issues:
I will close this for now since there is no evidence that the library has an issue here, please feel free to reply or open a new issue with a repository where I can see the issue and help you better.
I cloned the latest version of the repo and was able to reproduce the issue in the Multithreading example. Please try clicking on the graph a bunch of times, it should crash with an InvalidOperationException
on line 370 of the DataFactory.cs
class.
Which platform is it?
Avalonia 11. Was also breaking with the 0.10.x version. Sorry, forgot to mention this.
Ok, I can reproduce on Avalonia, thanks for the info. I will investigate, I have noticed that the Avalonia team improved a lot of things in v11, the sample I mentioned above was not even working in Avalonia 0.10, notice this issue does not happen on the rest of the platforms in this repo.
I need to find if the issue is on Live Charts or in the Avalonia side.
as a workaround just invoke the changes in the UI thread, I am sure that has no issues on Avalonia.
Thanks for investigating.
Yes, I can invoke in UI thread but posting on that thread 2000 times / sec causes performance issues on my end.
You were right, when you click the chart, it tries to invoke the chart events, but since the data is changing on another thread this exception is thrown, it is strange that it does not throw in the rest on the platforms 😥
The referenced commit fixes this, this is just for now, I think we can do much better and completely ignore the click event when there are no handlers subscribed to the chart events.
Thanks for the quick fix! When should I expect a build with this change to be published to NuGet?
It is included now in 2.0.0-beta.850.1
Describe the bug I have a chart which receives new data points at around 2KHz (2000/s) on a separate thread and plots them.
While performance is mostly fine, I noticed that when clicking on the chart, it will (after a couple clicks), produce a
System.InvalidOperationException (Collection Was Modified)
exception and crash. This is most likely due to one thread adding and removing data points from theObservableCollection<ObservablePoint>
array and the UI thread trying to read from the same collection.I'm using a thread lock and the
SyncContext
property to prevent this but the click handler code doesn't seem to utilize this. I have tooltips turned off as well.To Reproduce Steps to reproduce the behavior:
Expected behavior The chart should not crash.
Desktop:
Additional context After debugging, it seems like the problematic code is here, in
Kernel/Extensions.cs
.This LINQ expression does not lock the
SyncContext
object before it performs aSelect()
query.