Waiting for Document Conversion Service to be Ready to Convert

The Document Conversion Service must be running, either locally or on a remote computer for files or folders of files to be converted. If it is not running the ConvertFile, ConvertFolder, ConvertFileList and CombineFiles methods will all return immediately with an error.

In some scenarios, such as using these methods from another long running service, it may be desirable to wait for Document Conversion Service to be running instead of failing to convert the files.

This can be done in either of two ways:

use the IsConversionServiceRunning check to detect the running state in your code and wait in your own loop accordingly

pass a wait timeout value as a custom setting down the any of the methods to have the PEERNET.ConvertUtility wait

Detect Running State

The following code demonstrates using the IsConversionServiceRunning method to wait a maximum of fifteen minutes for the conversion service to be running. With this method, you can provide feedback or the ability to cancel on shorter intervals.

 

PNConversionItem resultItem = null;
Boolean bIsRunning = false;
int maxTimeout = 900000, // 15 minutes
    currentTimeout = 0; 
 
do
{
    if (PNConverter.IsConversionServiceRunning(String.Empty))
    {
        bIsRunning = true;
 
        resultItem = PNConverter.ConvertFolder(@"C:\Test\InputFiles",
                                               true, // include subfolders
                                               "*.pdf", // filter
                                               String.Empty, // exclude filter
                                               @"C:\Test\Output", // output folder
                                               true, // overwrite existing
                                               false, // remove file ext
                                               false, // create log
                                               "TIFF 200dpi OptimizedColor", // settings
                                               String.Empty, // extensison profile
                                               String.Empty, // MIME profile
                                               null, // User settings
                                               String.Empty, // not using remote conversion (DCOM)
                                               String.Empty, // use default working folder
                                               String.Empty);
    }
    else
    {
        if (currentTimeout < maxTimeout)
        {
            // Sleep for 30 seconds
            Console.WriteLine("Waiting for service to be available...");
            Thread.Sleep(30000);
            currentTimeout += 30000;
        }
        else
        {
            Console.WriteLine("Timeout on available service. No conversion performed.");
            bIsRunning = true;
        }
    }
} while (!bIsRunning );

 

Passing the Timeout Value to the PEERNET.ConvertUtility

The following code demonstrates passing the timeout value to the PEERNET.ConvertUtility to have the utility wait for the desired maximum of fifteen minutes for the conversion service to be running. In this case, if the conversion service is not running in the timeout value given the PNConversionItem object resultItem returned will contain the error message that the conversion service is not running.

 

PNConversionItem resultItem = null;
 
// Pass down how long to wait for the conversion service to be running.
Dictionary<String, String> customSettings = new Dictionary<String, String>();
customSettings["SecondsToWaitForRunningConversionService" ] = "900"; // 15 minutes max
 
resultItem = PNConverter.ConvertFolder(@"C:\Test\InputFiles",
                                       true, // include subfolders
                                       "*.pdf", // filter
                                       String.Empty, // exclude filter
                                       @"C:\Test\Output", // output folder
                                       true, // overwrite existing
                                       false, // remove file ext
                                       false, // create log
                                       "TIFF 200dpi OptimizedColor", // settings
                                       String.Empty, // extensison profile
                                       String.Empty, // MIME profile
                                       customSettings, // User settings
                                       String.Empty, // not using remote conversion (DCOM)
                                       String.Empty, // use default working folder
                                       String.Empty);