Navigation:  I Want To... > Miscellaneous >

Perform a background operation

Previous pageReturn to chapter overviewNext page

Use Case

You want to perform a long running operation in the background.

Relevant Architecture

None.

Relevant Types

BackgroundTask

Sample Code

using System.Threading;

using ClearCanvas.Common;

using ClearCanvas.Common.Utilities;

using ClearCanvas.Desktop;

using ClearCanvas.Desktop.Actions;

using ClearCanvas.Desktop.Tools;

 

// ... (other using namespace statements here)

 

namespace MyPlugin.Miscellaneous

{

   [ButtonAction("apply""global-toolbars/ToolbarStandard/PerformBackgroundOperationTool""Apply")]

   [IconSet("apply", IconScheme.Colour, "Icons.MyToolSmall.png""Icons.MyToolMedium.png""Icons.MyToolLarge.png")]

   // ... (other action attributes here)

   [ExtensionOf(typeof (DesktopToolExtensionPoint))]

   public class PerformBackgroundOperationTool : Tool<IDesktopToolContext>

   {

      public void Apply()

      {

         BackgroundTask task = new BackgroundTask(

            delegate(IBackgroundTaskContext context)

               {

                  for (int i = 0; i < 255; i++)

                  {

                     // BackgroundTaskProgress takes a percentage as an integer between 0 and 100

                     int percent = (int) ((i/255.0)*100);

 

                     BackgroundTaskProgress progress = new BackgroundTaskProgress(percent, string.Format("This is a status message ({0} out of 255)", i));

                     context.ReportProgress(progress);

 

                     if (context.CancelRequested)

                        break;

 

                     // Do a chunk of a large, time consuming task

                     Thread.Sleep(100);

                  }

               }, false); // this true indicates that the task is not cancellable; you can make it true if you want

 

         task.Run(); // this will run the task asynchronously (i.e. this call will return immediately)

      }

   }

}

Remarks

The BackgroundTask class can be used to make a long-running operation execute in the background.  If a reference to the BackgroundTask is kept, other code can programmatically query the task's progress and status messages (if any).  If the user must wait until the operation is complete (and hence a progress bar dialog should be displayed), please see Show a progress bar dialog.

Examples in Code Base

ClearCanvas.ImageViewer.Clipboard.ImageExport.AviExportComponent
ClearCanvas.ImageViewer.Clipboard.ImageExport.ImageExportComponent