Solved

Handle an event in the console application

Posted on 2004-04-13
8
1,115 Views
Last Modified: 2007-12-19
I'd like to using an Indy component in a console application.
As I cannot drag a component into a form, I created the conponent using

procedure Func(Sender: TObject; ......);
begin

end;

var UDPServ: TIdUDPServer;
begin
UDPServ:=TIdUDPServer.Create(nil);
UDPServ.OnUDPRead:=Func;           // is that right?
...
...
UDPServ.Free
end.

Can anyone give me an example how to handle the events in a component created dynamically.

thanks,

ysimon
0
Comment
Question by:ysimon
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 10822637
You're on the right track but you need to ensure that the event handler conforms to the requirements (e.g., parameters). The easiest way to do this is to do a mock-up as a normal windowed application and then cut-and-paste the resulting framework (e.g., the OnUDPRead) event.
0
 

Author Comment

by:ysimon
ID: 10824626
It looks not that easy.
I have made the correct parameters in the event procedure.
I got an error message on

UDPServ.OnUDPRead:=Func;           // is that right?

incompatible type: 'method pointer and regular procedure'

How should I define that event procedure?
For a window application (with a Form), this statement is in an object definition located in the .dfm file.

ysimon
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10825701
Try:

procedure Func(Sender: TObject; AData: TStream;
      ABinding: TIdSocketHandle) of object;
begin
...
end {Func};
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:ysimon
ID: 10828126
This doesn't work
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10829149
program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, IdUDPServer, IdSocketHandle;

type
  TMyUDPServer = class(TIdUDPServer)
  private
    procedure Func (Sender: TObject; AData: TStream;
                    ABinding: TIdSocketHandle);

  end;

var UDPServ: TMyUDPServer;

procedure TMyUDPServer.Func (Sender: TObject; AData: TStream;
                             ABinding: TIdSocketHandle);
begin
// Event handler code goes here
end;

begin
  UDPServ := TMyUDPServer.Create(nil);
  UDPServ.OnUDPRead := UDPServ.Func;           // is that right?
//...
//...
  UDPServ.Free
end.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 10847306
ysimon, I tested this solution and it appears to work for me. Have you tried it?
0
 
LVL 7

Accepted Solution

by:
sftweng earned 50 total points
ID: 13190271
My solution met the need, as tested.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now