Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 532

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

$
0
0

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)\Autodesk\Autodesk Vault 2014 SDK\VS10\CSharp\VaultBrowserSample 

 

privatevoid 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

                    <Framework.Currency.ProgressValue>

                                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:\temp\wbtest_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);

 

}


Viewing all articles
Browse latest Browse all 532

Trending Articles