Partager via


Step 1: Create a C# Sample Project that Sets Progress Information

Updated: August 2010

Applies To: Windows HPC Server 2008 R2

The following procedure describes how to create a sample application that sets the Progress and Progress Message job properties.

The sample application uses classes in the Microsoft.Hpc.Scheduler namespace to modify the job properties. For more information, see the HPC Class Library on MSDN (https://go.microsoft.com/fwlink/?LinkID=167944).

To create the sample project

  1. Run Visual Studio 2008.

  2. Create a new Visual C# Console Application that is named ProgressSample as follows:

    1. On the File menu, point to New, and then click Project.

    2. In the New Project dialog box, in Project Types, select Visual C#.

    3. In the list of templates, select Console Application.

    4. For the project name, type: ProgressSample.

    5. Click OK to close the dialog box and create the new project.

  3. Add a reference to the Microsoft.Hpc.Scheduler dynamic-link library (DLL) as follows:

    1. On the View menu, click Solution Explorer.

    2. In Solution Explorer, right-click References, and then click Add Reference.

    3. In the Add Reference dialog box, click the Browse tab.

    4. In the Look in drop-down menu, select the Bin folder for the HPC Pack 2008 R2 SDK. This is typically located at C:\Program Files\Microsoft HPC Pack 2008 R2 SDK\Bin.

    5. Select Microsoft.Hpc.Scheduler.dll.

    6. Click OK to add the reference and close the dialog box.

    7. In Solution Explorer, in References, verify that Microsoft.Hpc.Scheduler appears.

  4. In the Mayn source file, select all the code and then delete it.

  5. Paste the following code sample into the empty source file.

    using System;
    using System.Threading;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Hpc.Scheduler;
    
    namespace ProgressSample
    {
        class Program
        {
            static void Mayn(string[] args)
            {
                // Initialize variables
                double progress = 0;
                int iterations = 20;
                int iterationMilliseconds = 500;
    
                if (args.Count() == 2)
                {
                    iterations = System.Convert.ToInt32(args[0]);
                    iterationMilliseconds = System.Convert.ToInt32(args[1]);
                }
                else
                {
                    System.Console.Error.WriteLine("USAGE: ProgressSample.exe <NUM_ITERATIONS> <ITERATION_MILLISECONDS>");
                }
    
                // Discover the job's context from the environment
                String headNodeName = System.Environment.GetEnvironmentVariable("CCP_CLUSTER_NAME");
                int jobId = System.Convert.ToInt32(System.Environment.GetEnvironmentVariable("CCP_JOBID"));
    
                // Connect to the head node and get the job
                IScheduler scheduler = new Scheduler();
                scheduler.Connect(headNodeName);
                ISchedulerJob job = scheduler.OpenJob(jobId);
    
                // Run iterations and set progress
                for (int i = 1; i <= iterations; i++)
                {
                    // Do some work
                    Thread.Sleep(iterationMilliseconds);
    
                    // Set the progress
                    // Calculate the progress percentage
                    progress = (System.Convert.ToDouble(i) / System.Convert.ToDouble(iterations)) * 100;
                    // Set the progress percentage (must be an int between 0 - 100)
                    job.Progress = System.Convert.ToInt32(progress);
                    // Set a custom progress message (up to 80 chars)
                    job.ProgressMessage = i + " out of " + iterations + " steps complete.";
                    // Commit the change
                    job.Commit();
                }
            }
        }
    }
    
  6. On the File menu, click Save All.

  7. On the Build menu, click Build ProgressSample.

Continue to Step 2: Submit a Job that Runs ProgressSample.exe.