X-Sharp / XSharpPublic

Public repository for the source code for the XSharp Compiler, Runtime, Project System and Tools.
Apache License 2.0
113 stars 38 forks source link

Problem using DBServer objects in different threads #557

Open cpyrgas opened 3 years ago

cpyrgas commented 3 years ago

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
RobertvanderHulst commented 3 years ago

This is by design: Each thread has its own workareas and its own list of alias - area number - rdd objects.

cpyrgas commented 3 years ago

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)

RobertvanderHulst commented 3 years ago

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

wriedmann commented 3 years ago

This is the same purpose I had for my CoreDBF class.... maybe it could be a starting point. CoreDBF.zip