User Tools

Site Tools


k20:k20.11:start

20.11.0 Application

This Application (gb.gb) class provides information about the current process and the Gambas project running in that process.

The description of the property Application.Args under application aspects can be found in chapter → 5.8.1.1 Class Args in connection with options and arguments in Gambas programmes.

The class Application (gb) supplements the possibilities to read system information, which you can access via properties of the class System (gb) or the class of the same name Application (gb.qt4).

20.11.0.1 Properties

The Application (gb) class has these (static) properties, among others:

PropertyData typeDescription
DirIntegerReturns the working directory (CWD) when the application is started.
HandleIntegerReturns the system PID (system process identifier) of the programme.
IdIntegerSynonym for Application.Handle
NameStringReturns the programme name specified in the project properties dialogue. The name of the application is identical to the name of its project directory.
PathStringReturns the directory where the project is located.
TitleStringSpecifies the title of the application at runtime as defined in the project properties dialogue.
VersionStringIndicates the current programme version.
PriorityIntegerSets the process priority or reads the value. With Application.Priority Gambas uses the POSIX functions getpriority() and setpriority(). The value range is from -20 (highest priority) to +19. The default value is 0. Decreasing the priority is explicitly only allowed for privileged processes.
EnvEnvReturns a virtual collection of strings containing the process environment variables. The class Env has only the property Count (data type Integer).

Table 20.11.0.1.1 : Essential properties of the class gb.application

20.11.0.2 Property Application.Daemon

Set the Application.Daemon property to True to make the current process a daemon. To become a daemon:

  • the process splits (forks) itself (creates a copy) and then terminates itself. The copy, on the other hand, can run in the background as a system daemon,
  • it sets its current directory to “/” and
  • closes the standard input, standard output and standard error output.

Once you set the property to True, you can't undo that and set the property to False. The temporary directory - in Gambas each process has a working directory in /tmp/gambas.<UID>/<PID>/ - is renamed because the Process Identifier (PID) changes when it is split. Since a daemon works in the background and periodically, while a process that has a GUI should remain in the foreground and work (user) interactively, the two approaches are mutually exclusive.

20.11.0.3 Special Event Handler

The global event handlers described in the following paragraphs must be defined as static methods in the startup class to be considered by the interpreter.

→ Application_Error()

This event handler is called when an error occurs in the Gambas program that was not handled by a CATCH, TRY or FINALLY statement. It can be of use if, for example, a programme needs to bring a used resource into a defined state or to rescue data because the programme crashes unexpectedly. An example would be the IDE, which uses this method to save all open files when it crashes to prevent data loss.

Application_Error() is an emergency routine. The interpreter calls Application_Error() just before it exits itself due to the error. As a general rule, you should never terminate the programme yourself in Application_Error(). This method belongs to the part in the interpreter that handles a programme crash and must be able to act as such. Only the most necessary measures are to be taken by the programmer and the method should absolutely return without ME.Close or QUIT. In the event that an error occurs in the method itself, Application_Error() is not called again - the programme is terminated. You cannot access information about the error in Application_Error().

→ Application_Read()

Data is read that was sent to the standard input. If this static method was defined in the project start class, then the standard input is observed by the interpreter. The method is called each time data has been entered via the standard input. You can read the data, evaluate it and react according to the value.

20.11.0.4 Project

The project shows the use of Application_Read() as a special event handler. The internet radio client only plays the author's favourite station - but it does so with flying colours. So much sound for so little source code is quite remarkable:

' Gambas module file
 
Public mPlayer As New MediaPlayer
 
Public Sub Main()
 If mPlayer Then mPlayer = Null
 Start()
End
 
Public Sub Start()
 
  mPlayer = New MediaPlayer
  mPlayer.URL = "http://mp3channels.webradio.rockantenne.de/classic-perlen"
  mPlayer.Play()
  If mPlayer.Audio.Mute = True Then mPlayer.Audio.Mute = False
  FadeIn()
 
End
 
Public Sub Application_Read() '-- In a module *without* the keyword STATIC
 Dim sStandardInput As String
 
 Line Input #File.In, sStandardInput
 
 Select Case sStandardInput
   Case "q"
     FadeOut()
     mPlayer.Pause
     Print "Goodbye ... "
     Quit
   Case "p"
     mPlayer.Pause
   Case "r"
     mPlayer.Play
   Case "m"
     mPlayer.Audio.Mute = Not mPlayer.Audio.Mute
   Case "+"
     If mPlayer.Audio.Volume < 9.4 Then
        mPlayer.Audio.Volume += 0.5
     Endif
     Print "Volume = "; Round(mPlayer.Audio.Volume, -1)
   Case "-"
     If mPlayer.Audio.Volume > 0.6
        mPlayer.Audio.Volume -= 0.5
     Endif
     Print "Volume = "; Round(mPlayer.Audio.Volume, -1)
 End Select
 
End
 
Public Sub FadeIn()
  Dim fVolumeStart As Float = 2.0
 
  mPlayer.Audio.Volume = 0
  While mPlayer.Audio.Volume < fVolumeStart
    mPlayer.Audio.Volume += 0.05
    Print "*";
    Wait 0.2
  Wend
  Print
End
 
Public Sub FadeOut()
  While mPlayer.Audio.Volume > 0.2
    mPlayer.Audio.Volume -= 0.2
    Wait 0.1
  Wend
End

You get the contrast programme on this station:

  mPlayer.URL = "http://c22033l.i.core.cdn.streamfarm.net/22007mdrfigaro/live/3087mdr_figaro/live_de_128.mp3"

You start the client in the console alternatively like this:

  hans@linux:~/Rock4Me$ gbr3 ./r4m.gambas
  hans@linux:~$ gbx3 $HOME/Rock4Me
  • Use the p, r, m keys to control the Pause, Play (Run) and Mute functions of the client.
  • The FadeIn() and FadeOut() procedures raise and lower the volume at the start and end of the programme respectively.
  • You can change the volume at runtime with the - and + keys.
  • You end the console programme by entering q (Quit).
  • Each character is followed by an ENTER to activate it.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.11/start.txt · Last modified: 22.10.2023 by emma

Page Tools