Solved

Handle an event in the console application

Posted on 2004-04-13
8
1,149 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month5 days, 15 hours left to enroll

626 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