The threadKey parameter enables you to coordinate threaded calls. All calls made using the same thread key run on the same thread. By default, VisualAge uses the active process as the thread key so that all calls made from the same VisualAge process run on the same operating system thread. You can also supply a nil thread key. Let's see how it works:
(PlatformFunctions at: 'DosBeep')
coroutineCallWith: 440
with: 3000
threadKey: nil.
| context |
context := CgDisplay default handle.
(PlatformFunctions at: 'XBell')
coroutineCallWith: context
with: 50
threadKey: nil.
The coroutineCallWith: works just like a callWith: when the threadKey is nil. The external function is executed on the main VisualAge thread.
Let's try creating a thread and executing a couple of external functions on that thread.
| thread |
thread := [] fork. "Create a new suspended thread"
(PlatformFunctions at: 'DosBeep')
coroutineCallWith: 440
with: 3000
threadKey: thread.
(PlatformFunctions at: 'DosBeep')
coroutineCallWith: 999
with: 3000
threadKey: thread.
| context thread |
thread := 17. "Set thread to an arbitrary value"
context := CgDisplay default handle.
(PlatformFunctions at: 'XBell')
coroutineCallWith: context
with: 50
threadKey: thread.
(PlatformFunctions at: 'XBell')
coroutineCallWith: context
with: 50
threadKey: thread.
The first external function is executed under the new thread that was created. The second external function reuses that same thread. As a result, both external functions are executed under the separate thread associated with threadKey, and you are able to interact with other windows. For lengthy functions, be sure threadKey is not nil, otherwise users will not be able to interact with your application while the function is executing.