Article View: borland.public.cppbuilder.nativeapi
Article #4689Re: inherited Create;
From: Remy Lebeau (TeamB)
Date: Fri, 02 Dec 2005 19:50
Date: Fri, 02 Dec 2005 19:50
112 lines
3817 bytes
3817 bytes
"Alistair" <NoSpamPlease@Somewhere.Com> wrote in message
news:439005f7@newsgroups.borland.com...
> DoExecute() is a static member of my Base class
I wish you had said that earlier. That complicates matters a bit. Because
DoExecute() is static, then Execute() has to be static as well. Static
methods cannot be virtual, and thus cannot be overidden in derived classes
at all.
> which itself is a recreation class of TThread.
TThread doesn't have any static methods.
> I then create MyThread : public Base and override the
> virtual function, but it isn't executed.
See above.
> There is too much code to paste, so here is the idea of my class, it
> should compile unless I have missed some copy and pasting of needed code
> bool QLThread::Start()
Why are you qualifying Start() inside of BaseThread's declaration?
> DWORD WINAPI QLThread::ThreadExecute(BaseThread * pParam)
Again, why are you qualifying the method declaration inside of the class
declaration?
Also, you did not declare ThreadExecute() to be static. You cannot use
non-static class methods as API callbacks, such as with CreateThread()
(without using some really low-level assembler coding, that is). You should
be declaring ThreadExecute() to be static. Since you are already passing
the 'this' pointer to CreateThread(), ThreadExecute() will have the 'this'
pointer available to access non-static members.
> pParam->BaseThread::Execute();
Stop qualifying everything. You are not doing is correctly to begin with.
You don't need to qualify Execute() like that.
> pParam->Execute();
That is exactly what you should be doing.
Your code should look like this:
class BaseThread
{
public:
BaseThread()
: _hThread(NULL), _threadID(0)
{
}
bool Start()
{
_hThread = ::CreateThread(NULL, 0, &ThreadExecute, this,
adtdwFlags | CREATE_SUSPENDED, &_threadID);
if ( _hThread == NULL )
return false;
//...
::ResumeThread(_hThread);
return true;
}
protected:
virtual void Execute() = 0;
private:
HANDLE _hThread;
DWORD _threadID;
static DWORD WINAPI ThreadExecute(LPVOID lpParam)
{
try
{
((BaseThread*)lpParam)->Execute();
}
catch(...) {}
::ExitThread(0);
return 0;
}
};
class MyThread : public BaseThread
{
public:
MyThread()
: BaseThread()
{
}
protected:
virtual void Execute()
{
::MessageBox(0, "123", "456", MB_OK);
};
};
I tested this code and it works fine for me.
Gambit
Technical Details
Message-ID:
<4390a5d0$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<438e31c6$1@newsgroups.borland.com> <438e3b10$1@newsgroups.borland.com> <438eb24c$1@newsgroups.borland.com> <438ec656$1@newsgroups.borland.com> <439005f7@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3e5cce9567b943d3 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <438e31c6$1@newsgroups.borland.com> <438e3b10$1@newsgroups.borland.com> <438eb24c$1@newsgroups.borland.com> <438ec656$1@newsgroups.borland.com> <439005f7@newsgroups.borland.com> Subject: Re: inherited Create; Date: Fri, 2 Dec 2005 11:50:54 -0800 Lines: 114 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <4390a5d0$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1133553104 68.69.17.41 (2 Dec 2005 11:51:44 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail