| View previous topic :: View next topic |
| Author |
Message |
Brian Guest
|
Posted: Fri Feb 17, 2006 1:03 am Post subject: Writing an Event Handler |
|
|
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
|
Posted: Fri Feb 17, 2006 2:03 am Post subject: Re: Writing an Event Handler |
|
|
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
|
Posted: Fri Feb 17, 2006 4:03 pm Post subject: Re: Writing an Event Handler |
|
|
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
|
Posted: Fri Feb 17, 2006 5:03 pm Post subject: Re: Writing an Event Handler |
|
|
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
|
Posted: Fri Feb 17, 2006 9:03 pm Post subject: Re: Writing an Event Handler |
|
|
It looks like I put an extra * in there. Sorry about that.
- Clayton |
|
| Back to top |
|
 |
Clayton Arends Guest
|
Posted: Fri Feb 17, 2006 9:03 pm Post subject: Re: Writing an Event Handler |
|
|
"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 |
|
 |
|