Using Dao not in the main threaad!!!

Dear All,

I have used the AfxDaoInit() and AfxDaoTerm() in my worker thread to initialize the DAO Jet 3.5 engine.....

But I found if I perfrom some DAO operations in the main thread, and then closing the thread at the same time...
An error will be occured:

Unhandled exception in My.exe(DAO350.dll): 0xC0000005: Access Violation  
What is the problem .....how can I use the funciton AfxDaoTerm() when closing the thread???

I found if I haven't used the AfxDaoTerm() function after using the function AfxDaoInit() before opening the database, an error will also be occured. What can I do?

I know if I don't use any recordset object in my thread..no error will be occured. when I close the thread. However, I really need to access one database in my main thread and use a worker thread to access another also..



Thanks for your opinion!!!
cplauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vachoohoCommented:
All Dao objects should be closed and destroyed before the call to AfxDaoTerm(). Be sure you do not have dao objects in the stack (declared as local variables)

sample
THIS CODE IS WRONG

AfxDaoInit();
CDaoDatabase db;
db.Open(...);
CDaoRecordset rs(&db);
rs.Open(...);
....
rs.Close();
db.Close();
AfxDaoTerm();

CORRECT CODE

AfxDaoInit();
CDaoDatabase * pDB = new CDaoDatabase;
pDB->Open(...);
CDaoRecordset * pRS = new CDaoRecordset(pDB);
pRS->Open(...);
....
pRS->Close();
delete pRS;
pDB->Close();
delete pDB;
AfxDaoTerm();

Hope this helps

Also take a look at
http://www.codeguru.com/mfc_database/daothreadfix.html

The article has guidelines for using DAO in worker threads.


0
PIGCommented:
DAO i single thread. In this case You can use DAO from more threads only if have single thread access. AfxDaoInit and AfxDaoTerm can use only onece from one instance.

Make one worker thread with owner queue. Then all Your threads will be can communicate with that queue and in single input point.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.