我回电话说我是从固件中获取的。当我收到该回叫时,我想以适当的状态更新我的UI。所以我为每次回调都提出了属性更改事件,并且我在后台线程中订阅了该事件。
请在!WorkDone
中查看(DoWork
)时的相似内容。我阻止了调用,因为我希望后台线程保留在DoWork中直到更新完成(我应该使用ManualResetEvent
?)。问题是即使我在WorkDone
中将PropertyChanged
设置为true它永远不会被设置,并且我更新UI的CurrentStatus永远不会更新并且程序进入无限循环。请帮忙。
private void StartCurrentRun(bool obj)
{
this.worker = new BackgroundWorker();
this.worker.WorkerReportsProgress = true;
this.worker.WorkerSupportsCancellation = true;
StartTimer();
PropertyCallBackChangedInstance.PropertyChanged -= PropertyCallBackChangedInstance_PropertyChanged;
WhenCancelledBlurVolumesGrid = false;
OriginalTime = SelectedVolumeEstimatedTime();
this.worker.DoWork += this.DoWork;
this.worker.ProgressChanged += this.ProgressChanged;
this.worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
IsLiveProgress = true;
this.worker.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
try
{
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;
while (!WorkDone)
{
continue;
}
}
catch (Exception ex)
{
CreateEventLogs.WriteToEventLog(string.Format("{0} - {1}", "Error occured during Run", ex.Message), LogInformationType.Error);
}
}
private void PropertyCallBackChangedInstance_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
//bool stepDone = false;
if (e.PropertyName == "RunStepStatusName")
{
var value = sender as InstrumentCallBackProperties;
Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => {
CurrentStatus = value.RunStepStatusName;
if (value.RunStepStatusName == "Step5")
{
WorkDone = true;
}
}));
//stepDone = true;
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.ProgressValue = e.ProgressPercentage;
}
这是我调用它时的InitailizeRun方法我得到了回电
public async void InitalizeRun(VolumeInfo volumeInfo)
{
AssayInfo AssayInfo = new AssayInfo();
AssayInfo.IVolume = volumeInfo;
CartridgeStepStatus StepStatus = new CartridgeStepStatus();
StepStatus.Data = AssayInfo;
await Task.Run(() => _instrument.ProcessCartridge(StepStatus));
}
这是每当我得到它时的回电我正在更新属性
public void ProcessCartidge<T>(T data)
{
InstrumentCallBackPropertiesInstance.RunStepStatusName = data.ToString();
}
答案 0 :(得分:1)
您可以将BackgroundWorker
替换为Task.Run
,因为它与async
和await
无效。
Task.Run
启动后台线程,并且应该包含需要在单独的线程上运行的代码。如果要报告进度,则应使用IProgress参数。
这个例子可以让你朝着正确的方向前进。您可以基于此构建最终解决方案。
protected override async void OnLoadAsync( EventArgs e )
{
base.OnLoad( e );
try
{
IsLiveProgress = true;
await StartCurrentRunAsync( true );
}
catch ( Exception ex )
{
CreateEventLogs.WriteToEventLog( string.Format( "{0} - {1}" , "Error occured during Run" , ex.Message ) , LogInformationType.Error );
}
finally
{
IsLiveProgress = false;
}
}
private Task StartCurrentRunAsync( bool obj )
{
StartTimer();
PropertyCallBackChangedInstance.PropertyChanged -= PropertyCallBackChangedInstance_PropertyChanged;
WhenCancelledBlurVolumesGrid = false;
OriginalTime = SelectedVolumeEstimatedTime();
return Task.Run( () =>
{
CreateEventLogs.WriteToEventLog( string.Format( "Run with Assay:{0} Volume{1} has been started" , SelectedAssay , SelectedVolume ) ,
LogInformationType.Info );
var instance = ConnectToInstrument.InstrumentConnectionInstance;
return instance.InitalizeRun( PopulateRespectiveVolumes() );
} );
}
private void PropertyCallBackChangedInstance_PropertyChanged( object sender , PropertyChangedEventArgs e )
{
//bool stepDone = false;
if ( e.PropertyName == "RunStepStatusName" )
{
var value = sender as InstrumentCallBackProperties;
Dispatcher.CurrentDispatcher.BeginInvoke( ( Action ) ( () =>
{
CurrentStatus = value.RunStepStatusName;
if ( value.RunStepStatusName == "Step5" )
{
WorkDone = true;
}
} ) );
//stepDone = true;
}
}