Skip to Content

Long running task halts the SAP UI

I am using the C#API for UI and DI for SAP9.2 and working with forms. I created my forms as the SDK Help shows, but I am wondering how can I execute Tasks on different Thread than the UI Thread.

I need to load all the Items from OITM (28k) and run other queries as well, it usually completes in 5 seconds, but for the time it is running the SAP Completely stops being functional, you can't click on any other menu or do anything. Usually, when you write a desktop software with GUI, on one thread runs the GUI and you can do long running processes on a new background Thread and Display a progress bar on the gui. I should achieve something similar, not blocking the whole application. Can I display a progress indicator at least?

My Form calls my Data Access layer, passing the SBOApplication as a parameter, then creating a Company object from that. Than I run the queries and get the result as XML from the recordset (oRecordset.GetAsXML();) and parse the results. Should I just run the Queries on a different Thread myself in my addon and use the "Application.StatusBar.CreateProgressBar(...)" ProgressBar object to display the state?

I tried with the following code:

 Dal.Items itmsDao = new Dal.Items(SBOApp);
            ProgressBar oProgressBar = SBOApp.StatusBar.CreateProgressBar("Termékek betöltése...", 100, false);
            var worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += new DoWorkEventHandler(delegate (object o, DoWorkEventArgs args)
            {
                BackgroundWorker b = o as BackgroundWorker;
                var itemList = itmsDao.GetItems(itemParamters);
                b.ReportProgress(40);
                var filteredItems = FilterItems(itemList, itemParamters);
                b.ReportProgress(70);
                var arlsitaItems = FillPrices(filteredItems);
                b.ReportProgress(90);
                LoadDataToMainMatrix(arlsitaItems);
            });

            worker.ProgressChanged += new ProgressChangedEventHandler(delegate (object o, ProgressChangedEventArgs args)
            {
                oProgressBar.Value = args.ProgressPercentage;
            });

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate (object o, RunWorkerCompletedEventArgs args)
            {
                oProgressBar.Stop();
            });

           worker.RunWorkerAsync();

The Progress bar is displayed, the addon's UI won't respond to any interaction (i am okay with that), other parts of the SAP UI is working while it is processing. Is there a better way in SAP Addon development (best practice) or my approach should work in production?

Edit: Also, this solution has an error, It only works the first time, the second time it throws an Exception ( $exception {"Progress Bar - has stopped [66000-88]"} System.Runtime.InteropServices.COMException)

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Nov 03, 2016 at 03:42 PM

    Hi Szabolcs,

    Usually, when you write a desktop software with GUI, on one thread runs the GUI and you can do long-running processes on a new background Thread and Display a progress bar on the GUI.

    This is true, but B1 is not a usual application :(

    The API is single threaded and all operations run on the UI thread (you'll notice that B1 itself becomes unresponsive when loading big documents).

    In any case, you can run code on different threads (but you'll have to manage it carefully).

    I have a "guideline" which is to only perform operations on different threads when they are scoped to the DI API only (such as when creating a batch of documents).

    And yes, you can create progress bars to report progress to the user.

    Pedro Magueija

    LinkedIn | Twitter | Blog

    Add comment
    10|10000 characters needed characters exceeded