New task doesn't start

Post your Gambas programming questions here.
Post Reply
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

New task doesn't start

Post by 01McAc »

I am experimenting with tasks. The creation of virtual tables in the database for a full text search takes a bit of time. I would like to start the database operation as a new task to keep the program responsive for user interaction. But I got stuck and I have no idea why. The task in class MyTask_FTS simply doesn't start.

Here is what I have so far:
FMain.class
' Gambas class file
Public $hTask As MyTask_FTS

Public Sub Form_Open()
  Dim hImageIW As Image
  AG.Initialise
  hImageIW = Image.Load("Images/start.jpg")
  ImageView1.Image = hImageIW.Resize(ImageView1.Width - 20, ImageView1.Height - 20)  
  ImageView1.Refresh
  TaskRun()
  Print $hTask.Value
End

Private Sub TaskRun()  
' New task object - Object-Name = Object-Event-Name: MyTask_FTS
  If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS" 
  Repeat
    Wait 0.001
  Until $hTask <> Null  
  Application.Priority = 10 ' nice
End ' TaskRun()

MyTask_FTS.class
' Gambas class file
'MyTask_FTS.class

Inherits Task

Public Function Main() As Variant
  
  Dim DBS As New Cls_SQL_DataBase
  Dim $Rec As Result
  Dim $Query, $QueryUpdate As String 
  Dim sToday As String = Format(Now(), AV.FormatDBDateNoTime)  
  Debug sToday
  
  $QueryUpdate = "UPDATE App_variables SET Val='" & sToday & "' WHERE Var='LastCreateFTS'"
  Debug $Query  
    $Query = "SELECT * FROM App_variables WHERE Var='LastCreateFTS'"
    $Rec = DB.$Con.Exec($Query)
    If Not $Rec.Available Then 
      Message("No variable defined")
      Return False
    Endif
  
    If sToday > $Rec!Val Then
      Debug "Aktion: Create Virt. Table:  --> sToday '" & sToday & "' ist größer als der letzte Eintrag: '" & $Rec!Val & "'"
      DBS.Refresh_FTS
      $Rec = DB.$Con.Exec($QueryUpdate)
      Wait  
    Else
      Debug "No action required sToday '" & sToday & "' ist NICHT größer oder jedoch gleich letzter Eintrag: '" & $Rec!Val & "'"
    Endif
    Return True
End
Hmm, as I said already above the function Main() in class MyTask_FTS does not start. Does anyone know why?
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: New task doesn't start

Post by BruceSteers »

the function Main() only applies to Startup classes to use the function in your normal class you should change the name Main() to a command name.

then after
If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"

you can use $hTask.CommandName()

also you can use

Public Sub _new()
' Startup code here
End

any code within _new() is run when you create the object.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: New task doesn't start

Post by cogier »

If you are looking for task examples have a look on the Farm for InFile, it has 2 tasks and TaskExample.

You might find this thread interesting, we maxed out all 8 cores of a CPU using Task.
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

Re: New task doesn't start

Post by 01McAc »

BruceSteers wrote: Monday 8th March 2021 8:10pm then after
If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"

you can use $hTask.CommandName()
Cheers, it works and it speeds up the program in case of a complex DB operation. I have now an idea how tasks could work. BUT - what I don't understand yet - when I changed the name of the function from Main() to commandname() as proposed a runtime error occurred (Unable to find method Main in class MyTask_FTS.class). So I renamed it back to Main() and changed the $hTask.CommandName() into $hTask.Main().
The following sub TaskRun() is part of the FMain.class which might be the reason to name function Main() - ?
Private Sub TaskRun()  

' Create a new task object - Object name = Object-Event-Name: MyTask_FTS
  If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS" 
  $hTask.Main
  Repeat
    Wait 0.001
  Until $hTask <> Null  
  Application.Priority = 10 ' Prozess-Priorität für den Task
    
End ' TaskRun()
User avatar
BruceSteers
Posts: 1521
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: New task doesn't start

Post by BruceSteers »

01McAc wrote: Tuesday 9th March 2021 12:44pm
BruceSteers wrote: Monday 8th March 2021 8:10pm then after
If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"

you can use $hTask.CommandName()
Cheers, it works and it speeds up the program in case of a complex DB operation. I have now an idea how tasks could work. BUT - what I don't understand yet - when I changed the name of the function from Main() to commandname() as proposed a runtime error occurred (Unable to find method Main in class MyTask_FTS.class). So I renamed it back to Main() and changed the $hTask.CommandName() into $hTask.Main().
The following sub TaskRun() is part of the FMain.class which might be the reason to name function Main() - ?
Private Sub TaskRun()  

' Create a new task object - Object name = Object-Event-Name: MyTask_FTS
  If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS" 
  $hTask.Main
  Repeat
    Wait 0.001
  Until $hTask <> Null  
  Application.Priority = 10 ' Prozess-Priorität für den Task
    
End ' TaskRun()

aah , I have little experience using Tasks so it's possible i am mistaken as tasks actually work like startup classes?
Cogier has much more experience using tasks.

His InFile app might be a much better place to get help on this than me.

Ps. sometimes when using classes you have to hit the "Compile All" button in the ide to re-compile the classes to use.

glad you got it working :)
If at first you don't succeed , try doing something differently.
BruceS
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

Re: New task doesn't start

Post by 01McAc »

I'll have a look into the InFile project
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

Re: New task doesn't start

Post by 01McAc »

Post Reply