Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem using DBServer objects in different threads #557

Open
cpyrgas opened this issue Jan 20, 2021 · 4 comments
Open

Problem using DBServer objects in different threads #557

cpyrgas opened this issue Jan 20, 2021 · 4 comments

Comments

@cpyrgas
Copy link

cpyrgas commented Jan 20, 2021

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
Copy link
Member

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

@cpyrgas
Copy link
Author

cpyrgas commented Jan 20, 2021

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
Copy link
Member

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants