Complex example: unzipping

For a more complex implementation of the oProcess Worker, we can wrap around the zip executable in order to unzip an archive.

For this example, we will create a new object class in our library called oZip and inherit from the oProcess Worker:

oZip Object Class

Once we have our object class, we can start creating a new method: $unzip.

The $unzip method is going to be straightforward: if we're on Windows, call powershell.exe to Expand-Archive, otherwise if we're on either Linux or macOS, call out to the unzip executable. Of course, $unzip will also have to take in two parameters: the filepath to the zip archive we're trying to unzip and a destination path for the folder where the contents of the archive should be placed.

Once that is done, we just want to call $init() and $start() on our oProcess Worker.

We can implement the above in the following way:

Switch sys(6)
Case 'N' ## Windows
    Calculate lArgument as con('powershell.exe Expand-Archive -Path ',kDq,pZipFilePath,kDq,' -DestinationPath ',kDq,pDestinationPath,kDq)
Case 'X','U' ## macOS or Linux
    Calculate lArgument as con('unzip ',kDq,pZipFilePath,kDq,' -d ',kDq,pDestinationPath,kDq)
End Switch
Do $cinst.$init(lArgument)
Do $cinst.$start()

Since we are using $start, the executable is launched on a background thread: meaning Omnis code execution will continue in parallel, until the background worker finished its job, at which point it will call back into $completed.

Most often than not, you will want to run things on a background thread so the main thread doesn't block, which would make your application feel like it's getting stuck.

However, you will probably want to also know when something has finished running so that you can start working with the results.

In such scenarios, you should take a few extra arguments into your object class: an item reference which will point to the instance you wish to call back into and a character parameter for the method name to call.

For example, $unzip could take these parameters and save them as instance variables:

Calculate iCallbackMethod as pCallbackMethod
Set reference iCallbackInstance to pCallbackInstance

Afterwards, we can override the $completed() method inside our object and call back to our desired instance:

If pResultRow.retcode=0
    Do iCallbackInstance.[iCallbackMethod]('unzipped successfully')
Else
    Do iCallbackInstance.[iCallbackMethod]('an error has occurred')
End If

Of course, you can also easily pass parameters back to your callback method inside the round brackets.