aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2018-08-01 11:47:10 +0300
committerRoy Ben-Shabat <Roy@Twine-s.com>2018-08-01 11:47:10 +0300
commit00cfb0906e164487efed62bd6a65cdf681952bd1 (patch)
treee5bc283226a0f758d63da4c05f761ce7801ac36e /Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
parentd5c6067365e12674e95acaef4c4af45eaead8c3e (diff)
downloadTango-00cfb0906e164487efed62bd6a65cdf681952bd1.tar.gz
Tango-00cfb0906e164487efed62bd6a65cdf681952bd1.zip
Implemented reloading of color lab module and research module when machine designer changes machine !
Diffstat (limited to 'Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs')
-rw-r--r--Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs52
1 files changed, 48 insertions, 4 deletions
diff --git a/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs b/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
index 7c3ade821..e3575bfdc 100644
--- a/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
+++ b/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
@@ -6,6 +6,7 @@ using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Settings;
@@ -14,6 +15,8 @@ namespace Tango.BL
{
public partial class ObservablesContext
{
+ private List<ObservableModifiedEventArgs> _pending_notifications = new List<ObservableModifiedEventArgs>();
+
/// <summary>
/// Initializes a new instance of the <see cref="ObservablesContext" /> class.
/// </summary>
@@ -92,7 +95,48 @@ namespace Tango.BL
/// </returns>
public override int SaveChanges()
{
- return base.SaveChanges();
+ var result = base.SaveChanges();
+ RaisePendingNotifications();
+ return result;
+ }
+
+ /// <summary>
+ /// Asynchronously saves all changes made in this context to the underlying database.
+ /// </summary>
+ /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
+ /// <returns>
+ /// A task that represents the asynchronous save operation.
+ /// The task result contains the number of objects written to the underlying database.
+ /// </returns>
+ /// <remarks>
+ /// Multiple active operations on the same context instance are not supported. Use 'await' to ensure
+ /// that any asynchronous operations have completed before calling another method on this context.
+ /// </remarks>
+ public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
+ {
+ var result = base.SaveChangesAsync(cancellationToken);
+ RaisePendingNotifications();
+ return result;
+ }
+
+ /// <summary>
+ /// Raises the pending notifications.
+ /// </summary>
+ private void RaisePendingNotifications()
+ {
+ Task.Factory.StartNew(() =>
+ {
+ foreach (var e in _pending_notifications.DistinctBy(x => x.NotifiedEntity))
+ {
+ try
+ {
+ e.NotifiedEntity.RaiseModified(e.Context, e.ModifiedEntity, e.NotifiedEntity);
+ }
+ catch { }
+ }
+
+ _pending_notifications.Clear();
+ });
}
/// <summary>
@@ -109,10 +153,10 @@ namespace Tango.BL
{
IObservableEntity modified = entityEntry.Entity as IObservableEntity;
- Parallel.ForEach(ObservableEntitiesContainer.RegisteredEntities.Where(x => x.Guid == modified.Guid), (toNotify) =>
+ foreach (var toNotify in ObservableEntitiesContainer.RegisteredEntities.Where(x => x.Guid == modified.Guid))
{
- toNotify.RaiseModified(this, modified, toNotify);
- });
+ _pending_notifications.Add(new ObservableModifiedEventArgs(this, modified, toNotify));
+ }
}
return base.ShouldValidateEntity(entityEntry);