RunFree.class , Run methods without clogging the event handler

Ask about the individual Gambas components here.
Post Reply
User avatar
Posts: 685
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])


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


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

  $Parent = Parent
  $Method = Method
  $ArgArray = ArgArray


Static Public Sub Timer_Timer()

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


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"

Public Sub btnList_Click()
  Print "Exit Button Event handler"

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

I've been looking for a simple way to do this sort of thing for ages.
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
(19.8 KiB) Downloaded 56 times
Wishing well

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

Post Reply