Problem using DBServer objects in different threads
The following code tries to access a DBServer object from a different thread, than the one in which it was created, but it does not work, DBServer:Used returns FALSE and no db action can be performed on it. I guess this happens because the alias cannot be found in the new thread.
USING System.Threading
GLOBAL goDBserver AS DbServer
FUNCTION Start() AS VOID
LOCAL cDbf AS STRING
cDbf := "c:\test\abc"
DbCreate(cDbf , {{"FLD","C",10,0}})
goDBserver := DBServer{cDbf , FALSE}
? "Is it open in main thread?", goDBserver:Used // TRUE
LOCAL t AS Thread
t := Thread{MyThread}
t:Start()
System.Threading.Thread.Sleep(2000)
? "Is it still open in main thread?", goDBserver:Used // TRUE
? "Closing it now"
goDBserver:Close()
FUNCTION MyThread() AS VOID
? "Is it open in the new thread?", goDBserver:Used // FALSE
? "It is closed, so DBAppend() also returns", DbAppend() // FALSE
This is by design: Each thread has its own workareas and its own list of alias - area number - rdd objects.
So there's no way to make something like this work? By somehow sharing workareas among threads and changing the code in the DBServer class itself?
This makes it impossible to use DBServer with a BackgroundWorker, the problem Bernhard reported (I have pointed him to this ticket)
Impossible ? No. Difficult yes. The current design of the DbServer class uses the area number and alias internally. In the background thread these alias and number have no meaning. If it would use the RDD object then it would be easier. I'll see if we can redesign the DBServer class for this purpose.
Robert
This is the same purpose I had for my CoreDBF class.... maybe it could be a starting point. CoreDBF.zip