Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Handle an event in the console application

Posted on 2004-04-13
8
Medium Priority
?
1,162 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 100 total points
ID: 13190271
My solution met the need, as tested.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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