I want the layouts of some of my RadControls to be persisted in background via a new Thread/Task. The corresponding method is invoked so I can access the control that should be persisted without any error. But I'm always getting an exception at the time I call PersistenceManager.Save(control). Here's my code:
Can you please provide help why PersistenceManager.Save method is always throwing an exception here? How to use PersistenceManager to save a layout in background/new Task?
01.// --- Part of CustomRadGradView.cs (inherits from RadGridView) ---02.03.// Save this RadGridViews layout in background04.Task.Run(() =>05. {06. using (var manager = new CustomPersistenceManager())07. {08. // this is current RadGridView09. manager.SaveLayout(this, file);10. }11. }12.}01.// --- Part of CustomPersistenceManager.cs (inherits from PersistenceManager) ---02. 03.// Save a layout of any Telerik control04.public bool SaveLayout(Control control, string file = null)05.{06. // Current thread has no access to control -> Invoke required07. if (control.Dispatcher.CheckAccess() == false)08. {09. bool result = false;10. Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => result = DoSaveLayout(control, file)));11. return result;12. }13. 14. // Current thread has access to control15. return DoSaveLayout(control, file);16.}01.// --- Part of CustomPersistenceManager.cs (inherits from PersistenceManager) ---02. 03.// save layout04.private bool DoSaveLayout(Control control, string file = null)05.{06. // this lines work without any errors so method is invoked correctly; thread has access to control!07. string name = control.Name; // returns name of control08. control.Name = "New name"; // just for testing09. 10. // Next line always throws an InvalidOperationException11. using (var stream = Save(control)) // call save method of Teleriks PersistenceManager class12. {13. stream.Seek(0, SeekOrigin.Begin);14. 15. using (var reader = new StreamReader(stream))16. {17. string text = reader.ReadToEnd();18. File.WriteAllText(path, text);19. }20. }21.}Can you please provide help why PersistenceManager.Save method is always throwing an exception here? How to use PersistenceManager to save a layout in background/new Task?