public void InitMonitor(IDevice <bool> device)
 {
     Monitor = new ValueMonitor <bool>();
     this.RaisePropertyChangedOnUsingDispatcher(Monitor, () => PlcValue);
     Monitor.Sync(device);
     Name = device.Name;
 }
Example #2
0
 public void InitMonitor(IDevice <int> device)
 {
     Monitor = new ValueMonitor <int>();
     this.RaisePropertyChangedOnUsingDispatcher(Monitor, () => PlcValue);
     Monitor.Sync(device);
     IsPlcValueEnabled = true;
 }
	    public void Concurrent_Work_By_Multiple_Values_Is_Synchronized()
	    {
			// Arrange
			const int iterationsPerTestItem = 50;

			OperationCount = 0;
			GuidValueMonitor = new ValueMonitor<Guid>();

		    var testItems =
			    new List<TestItem>
			    {
				    new TestItem {Id = Guid.NewGuid(), MyValue = 0},
				    new TestItem {Id = Guid.NewGuid(), MyValue = 0},
					new TestItem {Id = Guid.NewGuid(), MyValue = 0},
				    new TestItem {Id = Guid.NewGuid(), MyValue = 0}
			    };

		    foreach (var testItem in testItems)
				WorkDataDictionary.Add(testItem, new List<WorkDataItem>(iterationsPerTestItem));

			var expectedOperationCount = iterationsPerTestItem * testItems.Count;

			var tasks = new List<Task>(expectedOperationCount);

			// Act
			for (var i = 0; i < iterationsPerTestItem; ++i)
			{
				for (var n = 0; n < testItems.Count; ++n)
				{
					var testItemIndex = n; // Avoid access to a modified closure.
					var task = Task.Run(() => WorkerMethod(testItems[testItemIndex]));
					tasks.Add(task);
				}
			}

		    foreach (var task in tasks)
			    task.Wait();

			Console.WriteLine("Total Operations .........: {0}", OperationCount);
			Console.WriteLine("ValueMonitor.LockCount ...: {0}", GuidValueMonitor.LockCount);
			Console.WriteLine();
		    WriteWorkDataToConsole();

			// Assert
			OperationCount.ShouldEqual(expectedOperationCount);
			GuidValueMonitor.LockCount.ShouldEqual(0);

		    foreach (var key in WorkDataDictionary.Keys)
		    {
			    var workDataItems = WorkDataDictionary[key];
				workDataItems.Count.ShouldEqual(iterationsPerTestItem);
		    }

			AssertWorkDataTimesAreCorrect();
	    }
Example #4
0
        public StateMonitor(System.Func <T> valueGetter, System.Action <T> onBegin, System.Action <T> onUpdate, System.Action <T> onEnd)
        {
            this.onBegin  = onBegin;
            this.onUpdate = onUpdate;
            this.onEnd    = onEnd;

            valueMonitor = new ValueMonitor <T>(valueGetter, (T from, T to) =>
            {
                OnEnd(from);
                OnBegin(to);
            });
        }
	    public void Concurrent_Work_By_A_Single_Value_Is_Synchronized_And_Handles_Disposal_Correctly()
	    {
			// Arrange
			const int iterations = 100;
		    const int disposeAfterCount = 50;

			OperationCount = 0;
			GuidValueMonitor = new ValueMonitor<Guid>();

		    var testItem = new TestItem {Id = Guid.NewGuid(), MyValue = 0};

			WorkDataDictionary.Add(testItem, new List<WorkDataItem>(iterations));

		    var tasks = new Task[iterations];

			// Act
			for (var i = 0; i < iterations; ++i)
			{
				tasks[i] = Task.Run(() => WorkerMethod(testItem));

				if (i == (disposeAfterCount - 1))
				{
					for (var n = 0; n < disposeAfterCount; ++n)
						tasks[n].Wait();
					GuidValueMonitor.Dispose();
				}
			}

		    foreach (var task in tasks)
			    task.Wait();

			Console.WriteLine("Total Operations .........: {0}", OperationCount);
			Console.WriteLine("ValueMonitor.LockCount ...: {0}", GuidValueMonitor.LockCount);
			Console.WriteLine();
			WriteWorkDataToConsole();

			// Assert
			ObjectDisposedExceptionCaught.ShouldBeTrue();
			OperationCount.ShouldEqual(disposeAfterCount);
			GuidValueMonitor.LockCount.ShouldEqual(0);
			WorkDataDictionary[testItem].Count.ShouldEqual(OperationCount);
			AssertWorkDataTimesAreCorrect();
	    }