New task doesn't start

Post

Posted
Rating:
#1 (In Topic #613)
Regular
01McAc is in the usergroup ‘Regular’
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

Code (gambas)

  1. ' Gambas class file
  2. Public $hTask As MyTask_FTS
  3.  
  4. Public Sub Form_Open()
  5.   Dim hImageIW As Image
  6.   AG.Initialise
  7.   hImageIW = Image.Load("Images/start.jpg")
  8.   ImageView1.Image = hImageIW.Resize(ImageView1.Width - 20, ImageView1.Height - 20)  
  9.   ImageView1.Refresh
  10.   TaskRun()
  11.   Print $hTask.Value
  12.  
  13. Private Sub TaskRun()  
  14. ' New task object - Object-Name = Object-Event-Name: MyTask_FTS
  15.   If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"
  16.     Wait 0.001
  17.   Until $hTask <> Null  
  18.   Application.Priority = 10 ' nice
  19. End ' TaskRun()


MyTask_FTS.class

Code (gambas)

  1. ' Gambas class file
  2. 'MyTask_FTS.class
  3.  
  4.  
  5.  
  6.   Dim DBS As New Cls_SQL_DataBase
  7.   Dim $Rec As Result
  8.   Dim $Query, $QueryUpdate As String
  9.   Dim sToday As String = Format(Now(), AV.FormatDBDateNoTime)  
  10.   Debug sToday
  11.  
  12.   $QueryUpdate = "UPDATE App_variables SET Val='" & sToday & "' WHERE Var='LastCreateFTS'"
  13.   Debug $Query  
  14.     $Query = "SELECT * FROM App_variables WHERE Var='LastCreateFTS'"
  15.     $Rec = DB.$Con.Exec($Query)
  16.     If Not $Rec.Available Then
  17.       Message("No variable defined")
  18.       Return False
  19.     Endif
  20.  
  21.     If sToday > $Rec!Val Then
  22.       Debug "Aktion: Create Virt. Table:  --> sToday '" & sToday & "' ist größer als der letzte Eintrag: '" & $Rec!Val & "'"
  23.       DBS.Refresh_FTS
  24.       $Rec = DB.$Con.Exec($QueryUpdate)
  25.       Wait  
  26.     Else
  27.       Debug "No action required sToday '" & sToday & "' ist NICHT größer oder jedoch gleich letzter Eintrag: '" & $Rec!Val & "'"
  28.     Endif
  29.     Return True

Hmm, as I said already above the function Main() in class MyTask_FTS does not start. Does anyone know why?
Online now: No Back to the top

Post

Posted
Rating:
#2
Guru
BruceSteers is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
01McAc is in the usergroup ‘Regular’

BruceSteers said

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() - ?

Code (gambas)

  1. Private Sub TaskRun()  
  2.  
  3. ' Create a new task object - Object name = Object-Event-Name: MyTask_FTS
  4.   If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"
  5.   $hTask.Main
  6.     Wait 0.001
  7.   Until $hTask <> Null  
  8.   Application.Priority = 10 ' Prozess-Priorität für den Task
  9.    
  10. End ' TaskRun()
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’

01McAc said

BruceSteers said

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() - ?

Code (gambas)

  1. Private Sub TaskRun()  
  2.  
  3. ' Create a new task object - Object name = Object-Event-Name: MyTask_FTS
  4.   If $hTask = Null Then $hTask = New MyTask_FTS As "MyTask_FTS"
  5.   $hTask.Main
  6.     Wait 0.001
  7.   Until $hTask <> Null  
  8.   Application.Priority = 10 ' Prozess-Priorität für den Task
  9.    
  10. 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 :)
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
01McAc is in the usergroup ‘Regular’
 I'll have a look into the InFile project
Online now: No Back to the top

Post

Posted
Rating:
#7
Online now: No Back to the top
1 guest and 0 members have just viewed this.