0

I have call back that comes as Property Changed Notifications from Firmware. Now in my code I want to wait till I hit the last property changed. I read the following post Manual Reset Event regarding ManualResetEvent but it says Manual Reset Events are used in case of multi threading. I am new to ManualResetEvents. This is my following code can I use the manual reset event in my case? if so how? If not whats the best way to wait there? Please help.

    //This is some button click action of RelayCommand
        private void StartCurrentRun(bool obj)
                {
                    this.worker = new BackgroundWorker();
                    this.worker.WorkerReportsProgress = true;
                    this.worker.WorkerSupportsCancellation = true;
                    OriginalTime = SelectedVolumeEstimatedTime();
                    StartTimer();
                    WhenCancelledBlurVolumesGrid = false;
                    //this.worker.DoWork += this.DoWork;
                    //this.worker.ProgressChanged += this.ProgressChanged;
                    //this.worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
                    //this.worker.RunWorkerAsync();
                    IsLiveProgress = true;
                    CreateEventLogs.WriteToEventLog(string.Format("Run with Assay:{0} Volume{1} has been started", SelectedAssay, SelectedVolume), LogInformationType.Info);
                    var instance = ConnectToInstrument.InstrumentConnectionInstance;
                    instance.InitalizeRun(PopulateRespectiveVolumes());
                    PropertyCallBackChangedInstance.PropertyChanged += PropertyCallBackChangedInstance_PropertyChanged;

        //Here I want to perform some action after I get a Processed state after the final property change event occurs. 
    //Can I do a manual reset event here.
                }

        private void PropertyCallBackChangedInstance_PropertyChanged(object sender, PropertyChangedEventArgs e)
                {
                    try
                    {
                        if (e.PropertyName == "InstrumentStatusChanged")
                        {
                            var value = sender as InstrumentCallBackProperties;
                            if (value.InstrumentStatusChanged == CurrentInstrumentStatus.Busy)
                            {
                                CurrentStatus = Application.Current.TryFindResource("Wait").ToString();
                            }
                        }
                        if (e.PropertyName == "RunStepStatusName")
                        {
                            var value = sender as InstrumentCallBackProperties;
                            CurrentStatus = EnumExtensions.GetDescription(value.RunStepStatusName);
                            NewProgressValue += 20;
                            UpdateProgress = true;
                        }

                        else if (e.PropertyName == "CurrentCartridgeStatusChanged")
                        {
                            var value = sender as InstrumentCallBackProperties;
                            if (value.CurrentCartridgeStatusChanged == CurrentCartridgeStatus.Processed)
                            {
                                PropertyCallBackChangedInstance.PropertyChanged -= PropertyCallBackChangedInstance_PropertyChanged;
                                EstimatedTimeRemaining = "00:00:00";

                                stopWatch.Stop();
                                timer.Stop();
                                IsLiveProgress = false;
                                CreateEventLogs.WriteToEventLog(string.Format("Run with Assay:{0} Volume{1} has been completed", SelectedAssay, SelectedVolume), LogInformationType.Info);

                                    if (IsRunSuccessfullyComplete != null && !WhenCancelledBlurVolumesGrid) //This indicates that Success will only open when the run is complete
                                    {
                                        IsRunSuccessfullyComplete();
                                    }
                                    WhenCancelledBlurVolumesGrid = true;

                                    if (ClearSelections != null)
                                    {
                                        ClearSelections();
                                    }
                            }
                        }

                    }
                    catch (Exception ex)
                    {
                        CreateEventLogs.WriteToEventLog(string.Format("Run with Assay:{0} Volume{1} failed", SelectedAssay, SelectedVolume), LogInformationType.Error);
                    }
                }
Nikhil ANS
  • 53
  • 1
  • 8

1 Answers1

0

It doesn't look to me like you want ManualResetEvent, which is used to signal multiple listeners. If your results were being produced by multiple tasks then you would use await WhenAll(...) however in your case the results are being informed by a property change not by a task completion. I would suggest that you simply record each property notification when it occurs, and check to see whether they are all complete. An easy way to do this is to use an enum with the [Flags] attribute: [Flags]public enum Completions { InstrumentStatusChanged, CurrentCartridgeStatusChanged, RunStepStatusName }. Just set the appropriate flag for each property callback and when you have set all the flags then you are done. If the property callbacks can occur in parallel or on different threads then you will need some form of locking such as a SemaphoreSlim(1,1).

sjb-sjb
  • 1,112
  • 6
  • 14