BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Writing an Event Handler

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
Brian
Guest





PostPosted: Fri Feb 17, 2006 1:03 am    Post subject: Writing an Event Handler Reply with quote



I am trying to write an event handler. Specifically, I have an
application that communicates via a COM port. I want an event
handler that will send me back the data stream with there is data
available.

I am using CreateFile...and ReadFile... where the ReadFile method
is handled in a thread. I want an OnData event that is triggered by the thread. The event needs to pass back the data that is in
the read buffer.

Can anyone post a small example of code showing how to write this
type of event handler?

Thanks in advance!
Back to top
Clayton Arends
Guest





PostPosted: Fri Feb 17, 2006 2:03 am    Post subject: Re: Writing an Event Handler Reply with quote



Some important design decisions that need to be made:

1) Should the event be thread-safe (called from the thread's execution
space) or should it be synced with the application thread?
2) Should the data be handed off quickly so that more data can be retrieved
from the device without waiting for the program to process each data packet?

Depending on how you answer these questions will lead your actual
implementation. But to answer the question as straightforward as possible
here is an example:

class TYourThread;
typedef void (__closure *TYourThreadDataEvent)(TYourThread* Sender, char*
ReadBuffer);
class TYourThread : public TThread
{
private:
TYourThreadDataEvent* fOnData;
...
public:
...
__property TYourThreadDataEvent OnData = { read = fOnData, write =
fOnData };
};

// calling the event from your thread (can be synchronized or
non-synchronized)
if (fOnData)
fOnData(this, data); // 'data' is the constructed

// application code, assigning the event handler (assume TForm1)
yourThread->OnData = &YourThreadData;

// declaration of YourThreadData (in class TForm1)
private:
void YourThreadData(TYourThread* Sender, char* ReadBuffer);

// definition
void TForm1::YourThreadData(TYourThread* Sender, char* ReadBuffer)
{
// do something with 'ReadBuffer'
}

I hope I didn't leave something out,

- Clayton
Back to top
Brian
Guest





PostPosted: Fri Feb 17, 2006 4:03 pm    Post subject: Re: Writing an Event Handler Reply with quote



I tried what you posted and am receiving the following errors
from the compiler:

Parameter mismatch in read access specifier of property OnData
Parameter mismatch in write access specifier of proterty OnData

How do I alleviate this?

Thanks.

"Clayton Arends" <nospam_claytonarends (AT) hotmail (DOT) com> wrote:
Quote:
class TYourThread;
typedef void (__closure *TYourThreadDataEvent)(TYourThread* Sender, char*
ReadBuffer);
class TYourThread : public TThread
{
private:
TYourThreadDataEvent* fOnData;
...
public:
...
__property TYourThreadDataEvent OnData = { read = fOnData, write =
fOnData };
};
Back to top
Brian
Guest





PostPosted: Fri Feb 17, 2006 5:03 pm    Post subject: Re: Writing an Event Handler Reply with quote

Thanks for your help. Here is what I ended up with:

In thread unit:

void __fastcall TRead::DisplayIt()
{
if(fOnData)
{ AnsiString data=AnsiString(InBuff);
fOnData(this,data);
}
}

In Header:

typedef void __fastcall (__closure *TOnDataEvent)(TObject* Sender,AnsiString &Buffer);

class TRead : public TThread
{
private:
TOnDataEvent fOnData;
char InBuff[1024];
__published:
__property TOnDataEvent OnData = { read=fOnData, write=fOnData };
protected:
void __fastcall Execute();
void __fastcall DisplayIt(void);
public:
__fastcall TRead(bool CreateSuspended);
};

"Clayton Arends" <nospam_claytonarends (AT) hotmail (DOT) com> wrote:
>here is an example:
Back to top
Clayton Arends
Guest





PostPosted: Fri Feb 17, 2006 9:03 pm    Post subject: Re: Writing an Event Handler Reply with quote

It looks like I put an extra * in there. Sorry about that.

- Clayton
Back to top
Clayton Arends
Guest





PostPosted: Fri Feb 17, 2006 9:03 pm    Post subject: Re: Writing an Event Handler Reply with quote

"Brian" <Brian (AT) nospam (DOT) org> wrote in message
news:43f5ff26$1 (AT) newsgroups (DOT) borland.com...

Quote:
Thanks for your help. Here is what I ended up with:

It looks fine. One thing to note is that although "__published" works it
really has no benefit beyond that of "public" in this class. To avoid
confusion you should reserve use of "__published" for controls/components or
if you intend on using the built-in VCL streaming at run-time.

- Clayton
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.