reStop Command

Purpose

Ends a thread that is performing a regular expression operation (used in a Try/Catch structure)

Syntax

reStop

Description

reStop is used within a thread auxiliary to the main program to stop that thread when it is performing a regular expression operation. reStop itself must always be used in a Try/Catch guarded code block.

NOTE: The reStop command is just one method of ending a thread and should be used with caution if at all. See Creating and Terminating Threads for more information on threads and how to close them.

Example

Local i%, j%, t#, m$(100000)

Global ti%, End?

// Create a random string array

For i% = 1 To 100000 : m$(i%) = RandomString : Next i%

Local h As Hash Int

// Create the Thread

Debug.Print "Thread Handle =";CreateThread(0, 0, ProcAddr(Thread), 0, 0, V:ti%) & "   "

Debug.Print "Thread ID =";ti%

// Perform the match

Try

t# = Timer

j% = reMatch(m$(), "[a-z]+n", 0, i% - 1, h[])

Catch

Debug.Print Err.Description

j% = -1

EndCatch

// Close the thread and print results

t# = Timer - t#

End? = True

While ti% : Sleep 30 : Wend : Sleep 0

Debug.Print h[%]; " Matches in"; t#; "s"

If j% < 0 Then Print "Aborted"

For Each j% In h[]

Debug.Print m$(j%)

Next

Debug.Show

Message "Results are on the Debug Screen"

 

Function RandomString

Local n As Int32, txt$

For n = 1 To Int(Rnd * 10) + 4 : txt$ = txt$ & Chr(Int(Rnd * 93) + 31) : Next n

Return txt$

EndFunction

 

Sub Thread(p%)

Debug.Print "Thread Started"

Do

If(GetAsyncKeyState(VK_ESCAPE) < 0)

reStop

Exit

EndIf

Sleep 100

Loop Until End?

ti% = 0

EndSub

The example program creates a random string array then searches the text for any succession of letters followed by the letter n.

Just before reMatch is executed - it should be placed in a Try/Catch/EndCatch construction - a second thread is created. The Debug.Print statement is unimportant; the only important thing is that the return value of the CreateThread() function non-zero.

When the regular expression search has ended, the global variable End? is set to True. This variable is inspected in the second thread and will cause the thread to end.

Meanwhile, inside the loop the thread tests for a key press (Esc key) and when it is pressed invokes the reStop command. reStop brings up an error message box (Err.Description = "reStop", Err.Number = 140). The error is trapped in the Try/Catch block in the main part of the program.

Remarks

NOTE: Breaking (pressing Ctrl-Break) a program which is using more than one thread or process will most likely result in an error in the IDE which will see GB32 closed down by Windows. Therefore, if you are testing such a program, always save it first.

See Also

Multithreading, preMatch, reMatch, reSub, Hash

{Created by Sjouke Hamstra; Last updated: 14/07/2015 by James Gaite}