comp • gb • task

Task (gb)

Since 3.3

This virtual class aims at being the parent class of all background tasks.

This class is not creatable.

Properties

Methods Events
Return the background task process id.
Handle  
Returns whether the task is running or not.
Running  
Returns the value returned by the background task.
Value  
Abort the background task.
Stop  
Wait for the background task's termination.
Wait  
This event is raised when the background task prints something on its error output.
Error  
This event is raised when the task terminates.
Kill  
This event is raised when the background task prints something on its standard output.
Read  

How does it work?

To run a task in the background:

  • You must create a class that inherits Task.

  • That class must have a public "Main" method that takes no argument.

  • You must instanciate the class to start a new task.

  • Then the Main method will be automatically run in the background at the next call of the event loop.

The Main method is run by a fork. It can access every other part of the program, except that the parent process will not see any change done by the task.

Many components will not like being forked. Especially the GUI ones. So be careful.

Sending arguments to the task

To send arguments to the task, just define some public variables in it, and fill them before the task starts.

Receiving a result from the task

The Main method can return any value that will be sent to the parent process through a serialization/deserialization internal process.

The value can be any value that you can use with the WRITE instruction.

The parent process just has to read the Value property to get that return value.

Task standard outputs

The task standard output (PRINT instruction) and error output (ERROR instruction) are automatically redirected, and the parent process will get them through the Read and Error events.

Task errors

If something bad happens during the task execution, the parent process can learn about it by reading the Value property, because an error event will be raised. The error message will, hopefully, explain the problem.

Task priority

To lower the priority of a task, use the Application.Priority property.