Re: One time code

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 functionality
( 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
-----------------
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • passing const ptr argument
    ... I want to write protect an argument that is passed by reference. ... But I still want to be able to call members of that argument. ... writeProtEvt.cpp: In member function `virtual void A::print': ...
    (comp.lang.cpp)
  • Re: why does this not compile ?
    ... Artie Gold wrote: ... >> virtual void a{ ... >> int main{ ... > reference to object, ...
    (comp.lang.cpp)