Solved

Handle an event in the console application

Posted on 2004-04-13
8
1,118 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

23 Experts available now in Live!

Get 1:1 Help Now