Use System.Threading.Tasks.Task with GetProgressView()

By Wayne Brill

It may not be obvious how to use GetProgressView() to report to the user the progress of something that takes some time. 

From the Vault API Help:

Autodesk.DataManagement.Client.Framework.Forms Namespace > Library Class : GetProgressView Method

Launches a new instance of a Progress Dialog

If you look at the help for ShowDialog method of the IProgressView Interface you will see that one of the arguments is a task form the System.Threading.Tasks:

void ShowDialog(
   System.Threading.Tasks.Task task,
   System.Threading.CancellationTokenSource cancelToken
)

This post has a good discussion about tasks.

http://johnbadams.wordpress.com/2012/03/10/understanding-cancellationtokensource-with-tasks/ 

Below is a c# example that shows the progress view and updates a couple of times until the task is done. This screenshot shows the dialog as the task is running.

image

 

To test this you can add a button to one of the SDK samples such as this one:

C:Program Files (x86)AutodeskAutodesk Vault 2014 SDKVS10CSharpVaultBrowserSample 

 

private void button3_Click(object sender, EventArgs e)    {        // test_IProgressView();        Framework.Forms.Interfaces.IProgressView                    myProgressView = null;             System.Threading.CancellationTokenSource            tokenSource =        new System.Threading.CancellationTokenSource();             var token = tokenSource.Token;        System.Threading.Tasks.Task taskWithToken =                    new System.Threading.Tasks.Task(            () =>            {                     while (true)                {                    //Wait - similates doing something                    System.Threading.Thread.Sleep(1000 * 5);                    // Need this for an argument to                     // ReportProgress to use it make a class                    // that derives from IEnumerable                    System.Collections.Generic.IEnumerable                                                            myProgressValues = null;                         int intPercentageComplete = 10;                    myProgressView.ReportProgress("Doing something",                                                "10 Percent complete",                            myProgressValues, intPercentageComplete);                         // See if the user hit cancel                    if (tokenSource.IsCancellationRequested)                    {                        break;                    }                         //This is going to do something with vault as a                     //test Change to something in your application                    string localPath = @"C:tempwbtest_7_22_14.txt";                    string fileName = "wbtest_7_22_14.txt";                    ACW.Folder vaultFolder = new ACW.Folder();                    string comment = "New Comment";                    ACW.File myFile = AddOrUpdateFile                        (localPath, fileName, vaultFolder, comment);                         if (tokenSource.IsCancellationRequested)                    {                        break;                    }                         intPercentageComplete = 30;                    myProgressView.ReportProgress("Doing Something",                                                "30 percent complete",                            myProgressValues, intPercentageComplete);                    //Wait - similates doing something                    System.Threading.Thread.Sleep(1000 * 5);                         intPercentageComplete = 80;                    myProgressView.ReportProgress("Doing Something",                                                "80 Percent complete",                            myProgressValues, intPercentageComplete);                         //Wait - similates doing something                    System.Threading.Thread.Sleep(1000 * 5);                         // Finish task                                         break;                     }            }, token        );             taskWithToken.Start();             myProgressView = Framework.Forms.Library.        GetProgressView("Doing Something", "Started");             myProgressView.ShowDialog                            (taskWithToken, tokenSource);         }

Comments

2 responses to “Use System.Threading.Tasks.Task with GetProgressView()”

  1. Klaus Loerincz Avatar
    Klaus Loerincz

    This is funny – As the standard vault progress dialogs are not this way:
    Vault 2015: Upload – Only shows number of uploaded file as progress.
     This is bad when uploading a big file – Users think Vault is frozen 
    Vault 2015: Download – Same thing as above.
    A real bummer when you try to explain this to an end user being exposed
    to so many other software handling this a lot more elegantly.

  2. Nice! Saved my day

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading