Skip to main content
Answer

how to keep process running until we stop manually.

  • December 16, 2021
  • 2 replies
  • 161 views

Forum|alt.badge.img

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

For an example of a similar pattern, check “Refresh Results” action in Request Profiler page (SM205070):
https://help-2021r2.acumatica.com/Help?ScreenId=ShowWiki&pageid=e7612f3f-fc6f-494d-8532-cc2ceef7147b

This page avoids the 5 seconds ajax refresh calls by having the end-user manually click on the refresh results button.

2 replies

Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • December 16, 2021

In my opinion the process must be redesigned. 

I don’t think it fits well in the way the framework works. 

 

 


Hughes Beausejour
Acumatica Employee
Forum|alt.badge.img+2
  • Acumatica Developer Support Team
  • Answer
  • December 17, 2021

For an example of a similar pattern, check “Refresh Results” action in Request Profiler page (SM205070):
https://help-2021r2.acumatica.com/Help?ScreenId=ShowWiki&pageid=e7612f3f-fc6f-494d-8532-cc2ceef7147b

This page avoids the 5 seconds ajax refresh calls by having the end-user manually click on the refresh results button.