RunFree.class , Run methods without clogging the event handler

Ask about the individual Gambas components here.
Post Reply
User avatar
BruceSteers
Posts: 652
Joined: Thursday 23rd July 2020 5:20pm

RunFree.class , Run methods without clogging the event handler

Post by BruceSteers » Saturday 28th August 2021 3:58pm

I have another very simple class for you here folks....

it's called RunFree.class
' Gambas class file
' RunFree.class written by Bruce Steers (with some help from Tobias Boege)

' Syntax..
' RunFree(Parent As Object, Method As String, ArgArray As Variant[])

' Parent is the class containing the Method (Function/Sub)
' Method is the Function/Sub name
' ArgArray is Variant Array[]
' Eg.
' RunFree(Me, "LengthyMethod",["Some Text", 22, True])


Export

Static Private $Timer As Timer

Static Private $Parent As Object
Static Private $Method As String
Static Private $ArgArray As Variant[]

Static Public Sub _init()
  
  $Timer = New Timer As "Timer"
  $Timer.Delay = 0

End


Static Public Sub _call(Parent As Object, Method As String, Optional ArgArray As Variant[])

  $Parent = Parent
  $Method = Method
  $ArgArray = ArgArray
  $Timer.Start

End

Static Public Sub Timer_Timer()

$Timer.Stop
Object.Call($Parent, $Method, $ArgArray)

End


It allows you to run a command freely in an event handler without blocking the gui calls because it is stuck in the event handler.

I have had/heard on many occasion Benoit say words to the effect of "You should not do that during an event handler!"
I think it has something to do with other events getting backlogged because you are locked in one event handler.

So here is a new way to run a function
Consider the 2 following Button event handler examples and the GetDir method they call...

One uses the standard way and the other uses the RunFree method.

Uppon click the standard way will report it has exited the GetDir method then exits the event handler when complete.
The RunFree method exits the event handler instantly and then the GetDir method runs and exits.

Public Sub btnListFree_Click()
  RunFree(Me, "GetDir", [DirBox1.Value])
  Print "Exit Button Event handler"
End

Public Sub btnList_Click()
  GetDir(DirBox1.Value)
  Print "Exit Button Event handler"
End

Public Sub GetDir(Path As String)
  TextArea1.Text = Dir(Path).Join("\n")
  Print "Exit GetDir() method"
End


I've been looking for a simple way to do this sort of thing for ages.
So..
RunFree(Me, "GetDir", [DirBox1.Value])
Me can be the name of any class/module
"GetDir" could be any Function/Sub method name
[DirBox1.Value] can be any number of any type of variable arguments

Here's hoping someone else may find it useful :)
Attached is a little test app and the class
Attachments
RunFree-1.0.1.zip
(19.8 KiB) Downloaded 9 times
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply