May 20, 2024, 03:51:09 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Multithreading

Started by Mr. O, May 22, 2008, 04:28:37 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Mr. O

Is there any way to multithread with dlls in emergence basic?

Mr. O

pistol350

Sure there is.
see this thread for instance :

http://www.ionicwind.com/forums/index.php/topic,2098.0.html

And here is a basic thread demo by Joske :


'thread demo
'find files in a directory with subdirectories
'ibasic pro
'written by jos de jong
'
'Threads can execute code separately from what your program is doing.
'For example if you use COPYFILE to copy a file of 500MB, your program would "hang" until it has finished this task.
'If you would execute COPYFILE in a thread, you can still use your program during copying the file (or you can show
'the progress of the copy in your program, or a cancel button, etc.).
'
'see also "Process and Thread Functions",
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/process_and_thread_functions.asp



'declare some functions needed for creating a thread
Declare import, CloseHandle(hObject:uint),int
Declare import, CreateThread(lpThreadAttributes As Int, dwStackSize As Int, lpStartAddress As UInt, lpParameter As UINT, dwCreationFlags As Int, lpThreadId As Pointer),Int
Declare import, TerminateThread(hThread:uint, dwExitCode As Int),Int
Declare import, SuspendThread(hThread:uint),Int
Declare import, ResumeThread(hThread:uint),Int
Declare import, ExitThread(dwExitCode As Int)

DEF threadID:INT
DEF hThread:INT :'handle of the thread

DEF searching:INT :'true if the program is busy with a search action, else false
DEF paused:INT :'true if the program is paused during a search action, else false
DEF count:INT :'contains the number of found files

CONST staSearchFolder = 5
CONST ctlSearchFolder = 6
CONST staSearchFilename = 7
CONST ctlSearchFilename = 8
CONST ctlSearch = 9
CONST ctlPause = 10
CONST ctlCancel = 11
CONST ctlSearchResults = 12
CONST staProgressLbl = 13
CONST staProgress = 14

'create a dialog with controls
DEF main:DIALOG
CREATEDIALOG main, 100,100,440,350, @SIZE | @MINBOX | @SYSMENU | @CAPTION, 0, "Search for file", &HandlerMain
CONTROL main, @STATIC, "Search folder", 10, 10, 80,20, 0, staSearchFolder
CONTROL main, @EDIT, "C:\\", 130, 10, 200,20, @TABSTOP | @CTEDITAUTOH, ctlSearchFolder
CONTROL main, @STATIC, "Search for filename", 10, 40, 120,20, 0, staSearchFilename
CONTROL main, @EDIT, "Eba", 130, 40, 120,20, @TABSTOP | @CTEDITAUTOH, ctlSearchFilename
CONTROL main, @BUTTON, "Seach", 340, 10, 80,24, @TABSTOP, ctlSearch
CONTROL main, @BUTTON, "Pause", 340, 40, 80,24, @TABSTOP, ctlPause
CONTROL main, @BUTTON, "Cancel", 340, 70, 80,24, @TABSTOP, ctlCancel
CONTROL main, @STATIC, "Progress:", 10, 80, 120,20, 0, staProgressLbl
CONTROL main, @STATIC, "", 130, 80, 200,20, 0, staProgress
CONTROL main, @LISTBOX, "", 10, 100, 410,230, @TABSTOP | @HSCROLL | @VSCROLL, ctlSearchResults

'set initial values for the global variables
searching = False
paused = False
count = 0


'show the dialog
DOMODAL main


WAITUNTIL main=0
END



'_______________________________________________
SUB HandlerMain
'handler for the dialog Main

SELECT @MESSAGE
CASE @IDINITDIALOG
EnableControls(searching, paused)

CASE @IDCLOSEWINDOW
'if there is a searchaction running, cancel it
IF searching THEN SearchCancel()

  'close the window
CLOSEWINDOW Main

CASE @IDCONTROL
IF @CONTROLID = ctlSearch THEN SearchStart()
IF @CONTROLID = ctlPause  THEN SearchPause()
IF @CONTROLID = ctlCancel THEN SearchCancel()

ENDSELECT

RETURN
ENDSUB


'________________________________________________________________________
SUB SearchStart()
'start a thread that will execute the search action

searching = True

hThread = CreateThread(0, 0, &SearchThread, 0, 0, &threadID)
IF hThread=0
CloseHandle(hThread)
searching = False
MESSAGEBOX 0, "Error: could not create thread", "Error"
ENDIF

EnableControls(searching, paused)

RETURN
ENDSUB

'________________________________________________________________________
SUB SearchPause()
'suspend or resume the search action

IF searching=True
IF paused=False
'suspend the search action
paused = True
SuspendThread(hThread)
ELSE
'resume the search action
paused = False
ResumeThread(hThread)
ENDIF
ENDIF

'update the controls
EnableControls(searching, paused)

RETURN
ENDSUB

'________________________________________________________________________
SUB SearchCancel()
'end the search thread (cancel it)

'end the thread
TerminateThread(hThread, 0)
CloseHandle(hThread)

searching = False
paused = False
EnableControls(searching, paused)
SETCONTROLTEXT(main, staProgress, "Search canceled.")

RETURN
ENDSUB


'________________________________________________________________________
SUB SearchThread()
'execute a search action as thread
DEF folder, searchtext:STRING

'get the folder and searchtext
folder = GETCONTROLTEXT(main, ctlSearchFolder)
searchtext = GETCONTROLTEXT(main, ctlSearchFilename)
IF folder<>"" THEN IF RIGHT$(folder,1)<>"\\" THEN folder+="\\"

'empty the results list
WHILE GETSTRINGCOUNT(main, ctlSearchResults)
DELETESTRING(main, ctlSearchResults, GETSTRINGCOUNT(main, ctlSearchResults)-1)
ENDWHILE
count = 0

'do the search action
SETCONTROLTEXT(main, staProgress, "Searching...")
findfile(folder, searchtext)

'search is finished
searching = False
EnableControls(searching, paused)
SETCONTROLTEXT(main, staProgress, "Search finished. " + USING("#", count) + " files found.")

'exit the thread
    ExitThread(0)

RETURN
ENDSUB

'________________________________________________________________________
SUB EnableControls(searhing:INT, paused:INT)
'enable and disable the right controls, depending on the values of searhing and paused
'searhing and paused can be True or False

ENABLECONTROL(main, ctlSearchFolder, (searhing=False))
ENABLECONTROL(main, ctlSearchFilename, (searhing=False))
ENABLECONTROL(main, ctlSearch, (searhing=False))
ENABLECONTROL(main, ctlPause, (searhing=True))
ENABLECONTROL(main, ctlCancel, (searhing=True))

IF paused=False
SETCONTROLTEXT main, ctlPause, "Pause"
ELSE
SETCONTROLTEXT main, ctlPause, "Resume"
ENDIF

RETURN
ENDSUB


'________________________________________________________________________
SUB findfile(path:STRING, searchstring:STRING),INT
'search for the given searchstring in the folder path with subdirectories
DEF dir,attrib:INT
DEF filename:STRING
DEF fullname:STRING

dir = FINDOPEN(path + "*.*")
IF(dir)
DO
filename = FINDNEXT(dir,attrib)
IF filename<>""
IF attrib & @FILE_DIRECTORY
'this is a directory
IF(filename <> ".") & (filename <> "..")
fullname = path + filename + "\\"
findfile(fullname, searchstring)
ENDIF
ELSE
IF INSTR(LCASE$(filename), LCASE$(searchstring))
'a file is found
ADDSTRING(main, ctlSearchResults, path + filename)
SETCONTROLTEXT(main, staProgress, USING("#", count) + " files found...")
count ++
ENDIF
ENDIF
ENDIF
'the exit case is when there are no more entries
'in the current directory
UNTIL filename = ""
FINDCLOSE dir
ENDIF
RETURN count
ENDSUB


Regards,

Peter B.