Reputation: 246
I am using an open source graphing library, oxyplot, in a c# wpf application. I am running into an issue where the UI thread is blocked when updating the graphs canvas object. Due to the canvas object belonging to the UI thread the update is done on the UI thread.
I have already moved all code I can to run on background threads, but the actual draw of the canvas isn't as easily solved.
The writer of the library tried with this piece of code using a scheduler from the UI thread's synchronization context but still couldn't get around the UI thread blocking when writing to the canvas.
The example above does work pretty much like the plotting library does so I won't give any of the code for that here.
How could I implement this without blocking the UI thread?
EDIT:
Here is a snippet of my code - it shows how i've attempted to implement this. You can see that I create the path data in the first bit of code. When i'm finished I attempt to loop round these on the UI Thread and add them to the canvas. However, once passed into my Add(p) call I get the error that the object belongs to another thread - even though I have past the pa.ToList() into the addToCanvas call...
.........
pa.Add(path);
}
Application.Current.Dispatcher.Invoke((Action)(() =>
{
addToCanvas(pa.ToList());
}));
}
public void addToCanvas(List<Path> path)
{
foreach (Path p in path)
{
Add(p);
}
}
Upvotes: 0
Views: 1812
Reputation:
I think you have to use a little hack here, something like a double buffering - create a buffer (byte, bitmap, etc.) and draw into it in the separate thread, and then place the stuff drawn at the control
Upvotes: 1