I saw Benjamin's post on multi-threading one command at a time and thought I'd complete his thought with a real multi-threading solution.
Benjamin's post can be found here:
http://forum.thegamecreators.com/?m=forum_view&t=121503&b=6
You'll need DLL 20 (Function Pointers) from IanM's Matrix1Utils plugin collection:
http://forum.thegamecreators.com/?m=forum_view&t=85209&b=18
MSDN says that this function will work only in Windows 2000 and up. However my Win32 Programmer's Reference says that it exists in Windows 95 but will only work when called from within a 32-bit program.
Sync must be on while using threads otherwise calls to DBProCore.dll may collide with another command being called internally, namely SYNC.
It's generally unsafe to use strings or any string related functions within a thread. Memory pointers to char strings should be used instead.
Mutexes (IanM's Matrix1Util DLL 3) should be used to lock access to individual commands or entire plugins to prevent collisions. It is recommended that only one command from each library be called at a time. Preferrably, only one command should be called from the entire DBP library at a time as all of its plugins share a common global structure.
My experience working with DBP and threads through the development of PureGDK has led me to the conclusion that is is generally unsafe to use threads in DBP because even when sync is on. This is because DBP either calls certain functions internally or uses the same resources as other functions which can lead to an invalid memory access crash when encountering key commands such as For/Next, Do/Loop, Sleep, Wait, etc.
Nevertheless it is possible but threads should be used at your own risk and with knowledge that it could lead to hard to random and debug crashes.
I highly recommend anyone interested in pursuing multi-threaded applications using DarkBasic Professional to look into PureGDK as an alternative. PureGDK supports not only multi-threading but can also compile thread-safe applications that use the DarkBasic Professional engine:
http://forum.thegamecreators.com/?m=forum_view&t=124255&b=8
http://www.purebasic.fr/english/viewtopic.php?t=31201
disable escapekey
sync on: sync sleep 1
sync: sync
Lib_Kernel32=load dll("Kernel32.dll")
ProcAddr_CreateThread=GET PTR TO DLL FUNCTION(Lib_Kernel32,"CreateThread")
ProcAddr_CloseHandle=GET PTR TO DLL FUNCTION(Lib_Kernel32,"CloseHandle")
remstart
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes
DWORD dwStackSize, // initial thread stack size, in bytes
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to returned thread identifier
);
remend
ThreadID=make memory(4)
hThread=call function ptr(ProcAddr_CreateThread,0,4,Get_Thread_Ptr(),0,ThreadID)
repeat
sync
sleep 1
until inkey$()<>""
` Cleanup
call function ptr Lib_Kernel32,ProcAddr_CloseHandle
delete memory ThreadID
end
function Get_Thread_Ptr()
ptr=get ptr to next function()
endfunction ptr
function Thead(Null)
repeat
sleep 250
print "Hello!"
until inkey$()<>""
endfunction
http://3dfolio.com