Skip to content

Best practices

Robert Peters edited this page Feb 5, 2015 · 7 revisions

This page contains some often used scenarios like importing data or showing a progress bar. These are all related to GAPP and not GAPPSF. GAPPSF has a totally different architecture and uses features of .NET 4.5 (like async and await).

Changing data in the background

Long operations should be performed outside the UI context to prevent blocking the User Interface. You start a new thread to perform the action, but before you do this, it should be prevented that events are triggered when changing the data in the thread. This is the scheme:

Starting the thread from within UI context

                    using (Utils.FrameworkDataUpdater upd = new Utils.FrameworkDataUpdater(Core))
                    {
                        await Task.Run(() => { threadMethod(); });
                    }

Thread method:

    private void threadMethod()
    {
        try
        {
              //do lengthy operation and change data
        }
        catch
        {
        }
        _actionReady.Set();
    }

Importing/Exporting data

A lot of times you have to import or export data. There are base plugins that helps you with this. Use Utils.BasePlugin.BaseImportFilter or Utils.BasePlugin.BaseExportFilter as a base class for your plugin.

From within the UI Context (most likely the Action method, you call PerformImport or PerformExport to start your background action. To implement your import or export, you override the ImportMethod or ExportMethod.

    protected override void ImportMethod()
    {
         //perform your action
    }

Progress bar

Long operations requires most of the time extra feedback to the user and optionally the option to stop the operation. For this, there is a progress bar is usefull. The progress bar activation should be done within a background thread.

example:

    protected override void ImportMethod()
    {
        using (Utils.ProgressBlock fixpr = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGDATA, _filenames.Length, 0))
        {    
             while(...)
             {         
                //perform your action
                //update progress bar
                fixpr.UpdateProgress(STR_IMPORTING, STR_IMPORTINGDATA, _filenames.Length, fileindex + 1);
             }
        }
    }

But you can also create a progress bar that lets the user cancel the operation.

example:

    protected override void ImportMethod()
    {
        using (Utils.ProgressBlock fixpr = new Utils.ProgressBlock(this, STR_IMPORTING, STR_IMPORTINGDATA, _filenames.Length, 0, true))
        {             
             while(...)
             {         
                //perform your action
                //update progress bar
                if (!fixpr.UpdateProgress(STR_IMPORTING, STR_IMPORTINGDATA, _filenames.Length, fileindex + 1))
                {
                    break;
                }
             }
        }
    }