【问题标题】:Using Manual Reset Event till last property changes使用手动重置事件直到最后一个属性更改
【发布时间】:2018-03-16 00:16:36
【问题描述】:

我收到了来自固件的属性更改通知的回电。现在在我的代码中,我想等到最后一个属性发生变化。我阅读了以下关于 ManualResetEvent 的帖子Manual Reset Event,但它说手动重置事件用于多线程的情况。我是 ManualResetEvents 的新手。这是我的以下代码我可以在我的情况下使用手动重置事件吗?如果是这样怎么办?如果不是,那么在那里等待的最佳方式是什么?请帮忙。

    //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);
                    }
                }

【问题讨论】:

    标签: c# wpf manualresetevent


    【解决方案1】:

    在我看来,您不需要 ManualResetEvent,它用于向多个侦听器发出信号。如果您的结果是由多个任务产生的,那么您将使用 awaitWhenAll(...) 但是在您的情况下,结果是通过属性更改而不是任务完成来通知的。我建议您只需在每个属性通知发生时记录它,并检查它们是否都完整。一个简单的方法是使用带有 [Flags] 属性的枚举:[Flags]public enum Completions { InstrumentStatusChanged, CurrentCartridgeStatusChanged, RunStepStatusName }。只需为每个属性回调设置适当的标志,当您设置所有标志时,您就完成了。如果属性回调可以并行或在不同线程上发生,那么您将需要某种形式的锁定,例如 SemaphoreSlim(1,1)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多