Re: One time code
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Thu, 06 Mar 2008 10:26:54 -0500
Well, you've not said enough to give a direct answer, but I'll try to inferr what is going
on here.
First, who decides when the action should be done? Document, view, what?
When is this decision made?
Why do you think you need a user-defined message to implement it?
Now, my first reaction to this was to think of having a single serial port control thread
shared by multiple documents. I'd probably implement it with a reference-counted
singleton class, such as
class Once {
public:
public:
Once() {
Error = ERROR_SUCCESS;
if(refcount == 0)
{ /* first time */
try {
Initialize();
}
catch(COnceException * e)
{
SetMyError(e->GetErrorCode();
e->Delete();
}
} /* first time */
else
Error = ERROR_ALREADY_EXISTS;
++refcount;
}
virtual ~Once() { --refcount;
if(recount == 0)
{ /* kill it */
Finalize();
} /* kill it */
}
class COnceException : public CExeception{
public:
CInitializeException(DWORD e) {Error = e;}
DWORD GetErrorCode() { return Error; }
protected:
DWORD Error;
};
DWORD GetMyError() { return Error; }
void SetMyError(DWORD e) { Error = e; }
protected:
virtual void Initialize() PURE;
virtual void Finalize() PURE;
static int refcount;
};
and in once.cpp:
int Once::refcount = 0;
(once.cpp is a very short file)
class MySerialPort : public Once()
public:
MySerialPort();
... more stuff here, as appropriate
protected:
HANDLE com;
...as appropriate
};
in MySerialPort.cpp
MySerialPort::MySerialPort() : Once()
{
}
void MySerialPort::Initialize()
{
com = ::CreateFile(...stuff...);
if(com == INVALID_HANDLE_VALUE)
{
DWORD err = ::GetLastError();
throw new COnceException(err);
}
...etc.
}
I typed this in off the top of my head, so it should be viewed with a bit of caution, but
it's the kind of code I might type in if I needed a singleton class that was reference
couned, but I didn't want to use reference-counted pointers to a dynamically-allocated
instance.
joe
On Thu, 6 Mar 2008 07:55:58 -0600, "Ron H" <rnharsh@xxxxxxxxxx> wrote:
In either an SDI or MDI application, if I want to include some functionalityJoseph M. Newcomer [MVP]
( such as serial port configuration and control thread) that can only be run
once per application instance, where should that code go? I looked at the
App class but it doesn't handle user messages ( at least by default ).
Should the main code and worker thread go in the MainFrm class in the
OnCreate function?
Help?
Ron H.
-----------------
www.Newsgroup-Binaries.com - *Completion*Retention*Speed*
Access your favorite newsgroups from home or on the road
-----------------
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- One time code
- From: Ron H
- One time code
- Prev by Date: Re: One time code
- Next by Date: Re: bug in common controls
- Previous by thread: Re: One time code
- Next by thread: PrintDialog GetPortName() truncated
- Index(es):
Relevant Pages
|