we are have a requirement to read the data from external API and add to grid, this should repeat in every 5 seconds and stop when we clicked stop.
Can someone please guide me the correct way to do this.
So, we created two buttons START and STOP and also a method to read from API and insert to grid.
Issue we are facing is - on start, say we are inserting 2 records, when we clicked on stop button, the records in the grid the getting duplicated, i mean, total 4 records.
Please have a look at below code.
public PXAction<DAC> startRead;
[PXUIField(DisplayName = "Start Scan", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Select)]
[PXButton(CommitChanges = true)]
public virtual IEnumerable StartRead(PXAdapter adapter)
{
System.Threading.Thread.Sleep(5000);
//cntLoop is unbound field with propery [PXUnboundDefault(true, PersistingCheck = PXPersistingCheck.Nothing)]
if (this.Document.Current.CntLoop == true)
{
PXLongOperation.StartOperation(this, delegate ()
{
AddItemsToGrid();
});
}
else
this.Save.Press();
return adapter.Get();
}
public PXAction<DAC> stopRead;
[PXUIField(DisplayName = "Stop Scan", MapEnableRights = PXCacheRights.Update, MapViewRights = PXCacheRights.Select)]
[PXButton()]
public virtual IEnumerable StopRead(PXAdapter adapter)
{
this.Document.Current.CntLoop = false;
RFIDScanStart(adapter);
return adapter.Get();
}
private void AddItemsToGrid()
{
//here we are reading data and inserting into grid
PXAdapter adapter = new PXAdapter(new PXView(this, true, this.Document.View.BqlSelect));
RFIDScanStart(adapter);
}
Best answer by Hughes Beausejour
View original