Gambas Documentation
Application Repository
Code Snippets
Compilation & Installation from source code
Components
gb
*[]
.Array.Bounds
.Stat.Perm
.Stream.Lines
.Stream.Term
.Symbol
_BoxedString
Application
Args
Array
Boolean[]
Byte[]
Class
Classes
Collection
Component
Components
Date[]
Enum
Env
Error
File
Float[]
gb
Integer[]
Jit
Long[]
Object
Object[]
Observer
Param
Pointer[]
Process
Short[]
Single[]
Stat
Stream
String
String[]
System
Task
Error
Handle
Kill
Kill
Read
Running
Stop
Value
Wait
Timer
User
Variant[]
gb.args
gb.cairo
gb.chart
gb.clipper
gb.complex
gb.compress
gb.crypt
gb.data
gb.db
gb.db.form
gb.db.mysql
gb.db.odbc
gb.db.postgresql
gb.db.sqlite2
gb.db.sqlite3
gb.db2
gb.dbus
gb.dbus.trayicon
gb.debug
gb.desktop
gb.desktop.gnome.keyring
gb.desktop.x11
gb.eval
gb.eval.highlight
gb.form
gb.form.dialog
gb.form.editor
gb.form.htmlview
gb.form.mdi
gb.form.print
gb.form.stock
gb.form.terminal
gb.gmp
gb.gsl
gb.gtk
gb.gtk.opengl
gb.gtk3
gb.gtk3.opengl
gb.gtk3.webview
gb.gui
gb.gui.opengl
gb.gui.qt
gb.gui.qt.ext
gb.gui.qt.opengl
gb.gui.qt.webkit
gb.gui.trayicon
gb.gui.webview
gb.hash
gb.highlight
gb.image
gb.image.effect
gb.image.imlib
gb.image.io
gb.inotify
gb.jit
gb.libxml
gb.logging
gb.map
gb.markdown
gb.media
gb.media.form
gb.memcached
gb.mime
gb.mongodb
gb.mysql
gb.ncurses
gb.net
gb.net.curl
gb.net.pop3
gb.net.smtp
gb.openal
gb.opengl
gb.opengl.glsl
gb.opengl.glu
gb.opengl.sge
gb.openssl
gb.option
gb.pcre
gb.pdf
gb.poppler
gb.qt4
gb.qt4.ext
gb.qt4.opengl
gb.qt4.webkit
gb.qt4.webview
gb.qt5
gb.qt5.ext
gb.qt5.opengl
gb.qt5.webkit
gb.qt5.webview
gb.qt6
gb.qt6.ext
gb.qt6.opengl
gb.qt6.webview
gb.report
gb.report2
gb.scanner
gb.sdl
gb.sdl.sound
gb.sdl2
gb.sdl2.audio
gb.settings
gb.signal
gb.term
gb.test
gb.util
gb.util.web
gb.v4l
gb.vb
gb.web
gb.web.feed
gb.web.form
gb.web.gui
gb.xml
gb.xml.html
gb.xml.rpc
gb.xml.xslt
Controls pictures
Deprecated components
Developer Documentation
Development Environment Documentation
Documents
Error Messages
Gambas Playground
How To's
Language Index
Language Overviews
Last Changes
Lexicon
README
Search the wiki
To Do
Topics
Tutorials
Wiki License
Wiki Manual

Task (gb)

Since 3.3

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

This class is not creatable.

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

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

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