How to create a multi-thread window
Posted: Wed Apr 08, 2015 10:39 am
Brian Wolfsohn suggested in this thread http://bb.donnay-software.com/donnay/vi ... 2&start=46 that it would be great to put some of his existing windows on multiple tab pages.
Each TabPage runs in its own thread.
Here is a sample program that shows how to do this.
Compile and run the below sample to see how it works.
Caution must be used when doing this as it is necessary to shut down the child threads when the parent window is closed.
Each TabPage runs in its own thread.
Here is a sample program that shows how to do this.
Compile and run the below sample to see how it works.
Caution must be used when doing this as it is necessary to shut down the child threads when the parent window is closed.
Code: Select all
#INCLUDE "dcdialog.CH"
#INCLUDE "appevent.CH"
FUNCTION Main()
LOCAL GetList[0], oMenu, oSubMenu, oDlg, GetOptions
DCMENUBAR oMenu
DCSUBMENU oSubMenu PROMPT 'File' PARENT oMenu
DCMENUITEM 'Multi-Threaded Window' ;
ACTION {|o|o := Thread():new(),Sleep(5),o:start({||MultiTab(oDlg:drawingArea)})} ;
PARENT oSubMenu
DCMENUITEM 'Single-Threaded Window' ;
ACTION {|o|o := Thread():new(),Sleep(5),o:start({||DataBrowse(oDlg:drawingArea)})} ;
PARENT oSubMenu
DCGETOPTIONS WINDOWWIDTH 1000 WINDOWHEIGHT 800
DCREAD GUI TITLE 'Multi-Threaded Window Test' PARENT @oDlg OPTIONS GetOptions
RETURN nil
* ----------
FUNCTION MultiTab(oParent)
LOCAL GetList[0], oTab1, oTab2, oTab3, aGetList[0], i, oDlg, oGetList
AAdd(aGetList,{})
AAdd(aGetList,{})
AAdd(aGetList,{})
DC_LoadRdds()
@ 0,0 DCTABPAGE oTab1 SIZE 100,25 CAPTION 'Tab 1'
@ 0,0 DCTABPAGE oTab2 CAPTION 'Tab 2' RELATIVE oTab1
@ 0,0 DCTABPAGE oTab3 CAPTION 'Tab 3' RELATIVE oTab2
DCREAD GUI FIT TITLE 'This is a Three-Thread Window' ;
APPWINDOW oParent ;
NODESTROY PARENT @oDlg ;
EVAL {|o|Sleep(20), ;
o := Thread():new(), Sleep(10), o:start({||DataBrowse(oTab1,aGetList[1])}),;
Sleep(20), ;
o := Thread():new(), Sleep(10), o:start({||DataBrowse(oTab2,aGetList[2])}),;
Sleep(20), ;
o := Thread():new(), Sleep(10), o:start({||DataBrowse(oTab3,aGetList[3])}),;
nil}
// Make sure all child threads are closed before destroying this window
FOR i := 1 TO Len(aGetList)
oGetList := DC_GetListObject(aGetList[i])
oGetList:isDialogActive := .f.
Sleep(10)
NEXT
oDlg:destroy()
RETURN nil
* ------------
PROC appsys ; RETURN
* ------------
FUNCTION DataBrowse(oParent,GetList)
LOCAL oBrowse
DEFAULT GetList := {}
USE \exp19\data\xtest VIA 'FOXCDX'
@ 2,2 DCBROWSE oBrowse ALIAS 'XTEST' SIZE 96,20 FONT '10.Lucida Console'
DCBROWSECOL FIELD XTEST->areacode WIDTH 10 PARENT oBrowse
DCBROWSECOL FIELD XTEST->exchange WIDTH 10 PARENT oBrowse
DCBROWSECOL FIELD XTEST->number WIDTH 10 PARENT oBrowse
DCBROWSECOL FIELD XTEST->date WIDTH 10 PARENT oBrowse
DCBROWSECOL FIELD XTEST->time WIDTH 10 PARENT oBrowse
@ 23,2 DCSAY 'This Browse is running in Thread ' + Alltrim(Str(ThreadID())) ;
SAYSIZE 0 FONT '12.Lucida Console'
IF oParent:isDerivedFrom("DC_XbpTabPage")
// All objects will be created on oParent. No dialog window.
DCREAD GUI PARENT oParent WAIT 5
ELSE
// All objects will be created on a new dialog window.
DCREAD GUI FIT APPWINDOW oParent TITLE 'This is a One-Thread Window'
ENDIF
dbCloseAll()
RETURN nil