Click here to Skip to main content
16,010,268 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralThread question Pin
achandra00712-Aug-02 9:27
achandra00712-Aug-02 9:27 
GeneralRe: Thread question Pin
Bijesh12-Aug-02 10:32
Bijesh12-Aug-02 10:32 
GeneralRe: Thread question Pin
achandra00712-Aug-02 10:43
achandra00712-Aug-02 10:43 
GeneralRe: Thread question Pin
Daniel Lohmann12-Aug-02 10:41
Daniel Lohmann12-Aug-02 10:41 
GeneralRe: Thread question Pin
achandra00712-Aug-02 11:11
achandra00712-Aug-02 11:11 
GeneralRe: Thread question Pin
Daniel Lohmann12-Aug-02 12:23
Daniel Lohmann12-Aug-02 12:23 
GeneralRe: Thread question Pin
achandra00712-Aug-02 12:27
achandra00712-Aug-02 12:27 
GeneralRe: Thread question Pin
Wes Jones12-Aug-02 12:05
Wes Jones12-Aug-02 12:05 
Hi Ashish,

Not to be rude, but there are plenty of problems w/ the code.

The access violoation you are seeing is due to the fact that CYourApp does not have a RunThread() function.
When you call AfxGetApp() is is returning a pointer to a CWinApp derived class, which is really a pointer to your CYourApp class which the class wizard creates for you. The ptr returned by AfxGetApp() is totally unrelated to any instances of CReadIntoSybaseDlg. The reason it didn't blow up on you when you used global data is because there wasn't any code implicitly trying to access the 'this' pointer in the implementation of RunThread. Since you're casting the ptr to CReadIntoSybaseDlg, your tricking it into calling your CReadIntoSybaseDlg::RunThread function. Your new function is trying to access member data via the 'this' ptr, but since you've casted the pointer to an invalid type, the 'this' ptr is really invalid.

Here's the proper way to call it:
CReadIntoSybaseDlg::StartThreadFunc()
{
   m_pThWorker = AfxBeginThread(TestThread, this);//<-- note, passing this 
                                                  //ptr, not 0
}
UINT CReadIntoSybaseDlg::TestThread(LPVOID lpvParam)
{
   CReadIntoSybaseDlg * pThis = reinterpret_cast<CReadIntoSybaseDlg*>(lpvParam);
  ASSERT( pThis );//if this goes off, you passed the wrong param to 
                  //AfxBeginThread

  pThis->RunThread();
  return 0;
}


Other things that would make your code a little nicer would be to rename the member variables w/ the m_ prefix.

I'd recommend that you, or anyone else reading this, read the book "Programming Windows Applications" by Jeffrey Richter, or even the earlier version of the book titled "Advanced Window Applications" or something like that.
Also, get a copy of the big red MFC book from Wrox press by Mike Blazncack (sp?).

Without knowing the rest of your code, you're probably going to be better of, performance wise, if you use a CCriticalSection instead of a CMutex.

I also want to warn you to be careful not to access the database objects from different threads at the same time, which is probably what your doing w/ the CMutex anyway. Bad things can happen if one thread's doing an insert and another's doing a rollback.
HTH,
-Wes


Sonork ID 100.14017 wtheronjones
GeneralRe: Thread question Pin
achandra00712-Aug-02 12:21
achandra00712-Aug-02 12:21 
GeneralRe: Thread question Pin
Wes Jones12-Aug-02 12:40
Wes Jones12-Aug-02 12:40 
GeneralRe: Thread question Pin
achandra00713-Aug-02 2:52
achandra00713-Aug-02 2:52 
GeneralRe: Thread question Pin
achandra00713-Aug-02 2:58
achandra00713-Aug-02 2:58 
GeneralRe: Thread question Pin
Wes Jones13-Aug-02 5:31
Wes Jones13-Aug-02 5:31 
GeneralRe: Thread question Pin
achandra00713-Aug-02 7:50
achandra00713-Aug-02 7:50 
GeneralMemory dump Pin
Hugo Hallman12-Aug-02 8:09
Hugo Hallman12-Aug-02 8:09 
GeneralRe: Memory dump Pin
PJ Arends12-Aug-02 15:09
professionalPJ Arends12-Aug-02 15:09 
Generalprintf question Pin
Shawn Horton12-Aug-02 8:10
Shawn Horton12-Aug-02 8:10 
GeneralRe: printf question Pin
Tomasz Sowinski12-Aug-02 8:21
Tomasz Sowinski12-Aug-02 8:21 
GeneralRe: printf question Pin
Shawn Horton12-Aug-02 8:39
Shawn Horton12-Aug-02 8:39 
GeneralRe: printf question Pin
Tomasz Sowinski12-Aug-02 8:54
Tomasz Sowinski12-Aug-02 8:54 
GeneralRe: printf question Pin
Shawn Horton12-Aug-02 9:01
Shawn Horton12-Aug-02 9:01 
GeneralRe: printf question Pin
Tomasz Sowinski12-Aug-02 9:09
Tomasz Sowinski12-Aug-02 9:09 
GeneralRe: printf question Pin
Daniel Lohmann12-Aug-02 10:34
Daniel Lohmann12-Aug-02 10:34 
GeneralRe: printf question Pin
Shawn Horton12-Aug-02 10:49
Shawn Horton12-Aug-02 10:49 
GeneralRe: printf question Pin
Daniel Lohmann12-Aug-02 12:28
Daniel Lohmann12-Aug-02 12:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.