?
Solved

Using Dao not in the main threaad!!!

Posted on 1999-10-29
2
Medium Priority
?
716 Views
Last Modified: 2013-11-20
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!!!
0
Comment
Question by:cplau
2 Comments
 
LVL 5

Expert Comment

by:vachooho
ID: 2170986
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
 
LVL 2

Accepted Solution

by:
PIG earned 160 total points
ID: 2171422
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question