Changeset 3416 for trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/AsynchronousStorableContentView.cs
- Timestamp:
- 04/19/10 21:00:30 (14 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm.WindowsForms/3.2/AsynchronousStorableContentView.cs
r3410 r3416 28 28 using System.Text; 29 29 using System.Windows.Forms; 30 using HeuristicLab.Common; 31 using System.Threading; 30 32 31 33 namespace HeuristicLab.MainForm.WindowsForms { 32 public partial class ContentView : View, IContentView { 33 private object content; 34 public object Content { 35 get { return content; } 36 set { 37 if ((value != null) && (!MainFormManager.ViewCanViewObject(this, value))) 38 throw new ArgumentException(string.Format("View \"{0}\" cannot view object \"{1}\".", this.GetType().Name, value.GetType().Name)); 39 if (InvokeRequired) { 40 Invoke(new Action<object>(delegate(object o) { this.Content = o; }), value); 41 } else { 42 if (this.content != value) { 43 if (this.content != null) this.DeregisterContentEvents(); 44 this.content = value; 45 if (this.content != null) this.RegisterContentEvents(); 46 this.OnContentChanged(); 47 } 48 } 49 } 34 public partial class AsynchronousStorableContentView : StorableContentView { 35 public AsynchronousStorableContentView() 36 : base() { 37 InitializeComponent(); 50 38 } 51 private bool saveEnabled; 52 public bool SaveEnabled { 53 get { return saveEnabled; } 54 protected set { 55 if (value != saveEnabled) { 56 saveEnabled = value; 57 OnChanged(); 58 } 39 public AsynchronousStorableContentView(IStorableContent content) 40 : base() { 41 this.Content = content; 42 } 43 44 /// <summary> 45 /// Asynchronous call of GUI updating. 46 /// </summary> 47 /// <param name="method">The delegate to invoke.</param> 48 protected new void Invoke(Delegate method) { 49 // prevents blocking of worker thread in Invoke, if the control is disposed 50 IAsyncResult result = BeginInvoke(method); 51 result.AsyncWaitHandle.WaitOne(1000, false); 52 if (result.IsCompleted) try { EndInvoke(result); } 53 catch (ObjectDisposedException) { } else { 54 ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, 55 new WaitOrTimerCallback((x, b) => { try { EndInvoke(result); } catch (ObjectDisposedException) { } }), 56 null, -1, true); 59 57 } 60 58 } 61 59 62 public ContentView()63 : base() {64 InitializeComponent();65 saveEnabled = true;66 }67 public ContentView(object content)68 : this() {69 this.content = content;70 }71 72 60 /// <summary> 73 /// A dds eventhandlers to the current instance.61 /// Asynchronous call of GUI updating. 74 62 /// </summary> 75 protected virtual void RegisterContentEvents() { 76 } 77 78 /// <summary> 79 /// Removes the eventhandlers from the current instance. 80 /// </summary> 81 protected virtual void DeregisterContentEvents() { 82 } 83 84 /// <summary> 85 /// Is called when the content property changes. 86 /// </summary> 87 protected virtual void OnContentChanged() { 63 /// <param name="method">The delegate to invoke.</param> 64 /// <param name="args">The invoke arguments.</param> 65 protected new void Invoke(Delegate method, params object[] args) { 66 // prevents blocking of worker thread in Invoke, if the control is disposed 67 IAsyncResult result = BeginInvoke(method, args); 68 result.AsyncWaitHandle.WaitOne(1000, false); 69 if (result.IsCompleted) try { EndInvoke(result); } 70 catch (ObjectDisposedException) { } else { 71 ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, 72 new WaitOrTimerCallback((x, b) => { try { EndInvoke(result); } catch (ObjectDisposedException) { } }), 73 null, -1, true); 74 } 88 75 } 89 76 }
Note: See TracChangeset
for help on using the changeset viewer.