Progress Tracker Dialog

From OpenPetra Wiki
Jump to navigation Jump to search

purpose

When you run a long-running job through the webconnector, the user might be confused why the client "hangs". It is better to give a dialog with a progress bar, show status messages, and allow the user to cancel the job.

sample use

See the import of the yml.gz databases, in System Manager.

Server Side: http://bazaar.launchpad.net/~openpetracore/openpetraorg/trunkhosted/view/head:/csharp/ICT/Petra/Server/lib/MSysMan/ImportExport.cs

Client Side: http://bazaar.launchpad.net/~openpetracore/openpetraorg/trunkhosted/view/head:/csharp/ICT/Petra/Client/MSysMan/Gui/SysManMain.cs

how to use it

server side

At the beginning of the webconnector function, initialise the progress tracker. This specifies the current client, the caption for the progress dialog window, and the overall absolute amount, which should correspond with 100%.

  TProgressTracker.InitProgressTracker(DomainManager.GClientID.ToString(),
               Catalog.GetString("Importing database"),
               tables.Count);

Then in a loop, you can update the progress percentage, and the status message:

  TProgressTracker.SetCurrentState(DomainManager.GClientID.ToString(),
                       String.Format(Catalog.GetString("loading table {0}"), table),
                       tableCounter);

In such a loop, you could also check if the user has pressed the cancel button in his dialog:

  if (TProgressTracker.GetCurrentState(DomainManager.GClientID.ToString()).CancelJob == true)
  {
      DBAccess.GDBAccessObj.RollbackTransaction();
      return false;
  }

At the end, you need to finish the job, so that the progress dialog on the client side will close:

  TProgressTracker.FinishJob(DomainManager.GClientID.ToString());

client side

On the client side, you need to put the call to the webconnector into a separate function, that will run it is own thread:

  private static bool WebConnectorResult = false;
  
  private static void ResetDatabaseInThread(string AZippedYml)
  {
      WebConnectorResult = TRemote.MSysMan.ImportExport.WebConnectors.ResetDatabase(AZippedYml);
  }

Then you start the thread, and open the progress dialog:

   Thread t = new Thread(() => ResetDatabaseInThread(zippedYml));
   t.Start();

   TProgressDialog dialog = new TProgressDialog();

   if (dialog.ShowDialog() == DialogResult.Cancel)
   {
       return;
   }

   if (WebConnectorResult)
   {
       // TODO: reset all caches? for comboboxes etc
       MessageBox.Show(Catalog.GetString("Import of database was successful. Please restart your OpenPetra client"));
   }
   else
   {
       MessageBox.Show(Catalog.GetString("Failed import of database. Please check the Server.log file on the server"));
   }