I'm not using threads in my app, but I receive an error "Cannot use a DependencyObject that belongs to a different thread than its parent Freezable.", when I'm trying to do the following:
namespace WpfApplication1{ public class GrObject { public Rect Rect { get; set; } public Pen Pen { get; set; } public void draw(DrawingContext dc) { dc.DrawRectangle(null, Pen, Rect); } } public partial class Window1 : Window { IObjectContainer db; public Window1() { InitializeComponent(); db = Db4oFactory.OpenFile("c:\\temp.db4o"); try { GrObject o = new GrObject(); o.Rect = new Rect(new Point(10, 10), new Size(300, 200)); o.Pen = new Pen(Brushes.Red, 5); db.Store(o); } catch (Exception ee) { } finally { db.Close(); // Storing the data } db = Db4oFactory.OpenFile("c:\\temp.db4o"); // Opening again } protected override void OnRender(DrawingContext dc) { base.OnRender(dc); IEnumerable<GrObject> p = from GrObject o in db select o; foreach (GrObject o in p) { o.draw(dc); } } ~Window1() { db.Close(); } }}
You're problem is almost certainly that you try to store instances of WPF-classes. (For example the Brushes.Red-instance). All WPF and Winforms-classes are closly coupled to the rendering-pipeline. So therefore instances of UI-framework-classes have references to whole rendering-system and the native os-resource (for examples handles). Such classes cann't be stored by db4o. Well db4o will trie but fail.
So finally to I'm comming to the point: You have to carfully design and choose your domain-model-classes. Avoid any GUI-classes in you domain-model. For example: I'm currently developing a WPF-application myself and i also store UI-related information such as colors. However I don't use the WPF-color-class, but my own Color-Class for storing the information.
Actually I don't think that it's too complicate for db4o to store UI class objects.
I think I find out the problem, but couldn't solve it yet. If I put the following in GrObject.draw():
Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId+" "+this.Pen.Dispatcher.Thread.ManagedThreadId);
it shows that my Pen is in the different thread! So if I understand right, when I'm querying the data from db4o, complex members returns made in another thread... can someone explain that?