Task (gb)

Sinds 3.3

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

Deze klasse is niet creƫerbaar.

Eigenschappen
Handle   Return the background task process id.
Running   Returns whether the task is running or not.
Value   Returns the value returned by the background task.

Methode
Kill   A synonymous of the Stop method.
Stop   Abort the background task.
Wait   Wait for the background task's termination.

Gebeurtenissen
Error   This event is raised when the background task prints something on its error output.
Kill   This event is raised when the task terminates.
Read   This event is raised when the background task prints something on its standard output.

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 instantiate 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.

Hopefully, Gambas tries to be as nice as possible with them in that case. But be careful anyway.

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.

Alternatively you an use the tasks _new( ... ) method to pass the arguments when you instantiate it.

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.