在控制台应用程序中处理事件

在控制台应用程序中处理事件

本文介绍了Delphi 7:在控制台应用程序中处理事件(TidIRC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于Indy的IRC组件(TIdIRC)制作一个控制台应用程序,但我遇到问题。这是我的代码:

I'm trying to make a console application based on Indy's IRC Component (TIdIRC) but I'm having trouble with events. Here's my code:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  Math,
  IdBaseComponent,
  IdComponent,
  IdTCPConnection,
  IdTCPClient,
  IdIRC;

type
  TEvents = class
  public
    procedure Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
  end;

const
  IrcServ = 'gr.irc.gr';
  IrcPort = 6667;
  IrcChan = '#lalala';

var
  Irc: TidIRC;
  Event: TEvents;
  uName, rName: string;

function Log(s: string): string;
var now: TDateTime;
begin
  now := Time;
  result := FormatDateTime('[hh:nn:ss] ', now) + s;
end;

procedure TEvents.Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
begin
  Log(AUser.Nick+' '+ACommand+' '+AContent);
end;

begin
  Event := TEvents.Create;
  Irc := TidIRC.Create(nil);
  Irc.OnRaw := Event.Raw;
  Randomize;
  Write('Nickname: ');
  ReadLn(uName);
  rName := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
  with Irc do begin
    AltNick := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
    Nick := rName;
    Username := rName;
    RealName := 'I.D.M.';
    Host := IrcHost;
    Port := IrcPort;
    //MaxLineAction := maException;  <-- [ERROR] Undeclared identifier: 'maException'
    ReadTimeout := 0;
    UserMode := [];
    Connect();
    Join(IrcChan);
  end;
  ReadLn;

end.

我已经尝试了所有我可以想到的一切,但是尽管应用程序连接成功,

I've tried so far everything i could think of, but, although the app is connected successfully, it won't return any raw message... What am i missing?

推荐答案

TdIRC

TdIRC uses an internal worker thread to receive data. The OnRaw event is triggered when that thread is parsing data. The thread uses TThread.Synchronize() to do that parsing. Since your main thread does not have an active VCL message loop, you can pump the Synchronize() queue manually. After you connect, call the CheckSynchronize() function from the Classes unit in a loop while you are connected to IRC, eg:

begin
  ...
  Connect;
  try
    Join(IrcChan);
    do
      CheckSynchronize;
      Sleep(10);
    until SomeCondition;
  finally
    Disconnect;
  end;
  ...
end.

为了很好的措施,您可以为 WakeMainThread CheckSynchronize() Classes 单元中的c $ c>事件,因此主线程可以在IRC连接空闲时进入睡眠状态,例如:

For good measure, you can assign a handler to the WakeMainThread event in the Classes unit to help control when CheckSynchronize() should be called, so the main thread can go to sleep while the IRC connection is idle, eg:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Classes,
  Math,
  IdBaseComponent,
  IdComponent,
  IdTCPConnection,
  IdTCPClient,
  IdIRC;

type
  TEvents = class
  private
    FSyncEvent: TEvent;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
    procedure Wake(Sender: TObject);
    procedure CheckSync;
  end;

function Log(s: string): string;
begin
  result := FormatDateTime('[hh:nn:ss] ', Time) + s;
end;

constructor TEvents.Create;
begin
  inherited;
  FSyncEvent := TEvent.Create(nil, False, False, '');
end;

destructor TEvents.Destroy;
begin
  FSyncEvent.Free;
  inherited;
end;

procedure TEvents.Raw(Sender: TObject; AUser: TIdIRCUser; ACommand, AContent: String; var Suppress: Boolean);
begin
  Log(AUser.Nick+' '+ACommand+' '+AContent);
end;

procedure TEvents.Wake(Sender: TObject);
begin
  FSyncEvent.SetEvent;
end;

procedure TEvents.CheckSync;
begin
  FSyncEvent.WaitFor(Infinite);
  CheckSynchronize;
end;

const
  IrcServ = 'gr.irc.gr';
  IrcPort = 6667;
  IrcChan = '#lalala';

var
  Irc: TidIRC;
  Event: TEvents;
  uName, rName: string;

begin
  Event := TEvents.Create;
  try
    WakeMainThread := Event.Wake;
    Irc := TIdIRC.Create(nil);
    try
      Irc.OnRaw := Event.Raw;
      Randomize;
      Write('Nickname: ');
      ReadLn(uName);
      rName := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
      with Irc do begin
        AltNick := 'IDM' + IntToStr(RandomRange(1000, 9999)) + uName;
        Nick := rName;
        Username := rName;
        RealName := 'I.D.M.';
        Host := IrcHost;
        Port := IrcPort;
        //MaxLineAction := maException;  <-- [ERROR] Undeclared identifier: 'maException'
        ReadTimeout := 0;
        UserMode := [];
        Connect;
        try
          Join(IrcChan);
          do
            Event.CheckSync;
          until SomeCondition;
        finally
          Disconnect;
        end;
      end;
    finally
      Irc.Free;
    end;
  finally
    Event.Free;
  end;
end.

这篇关于Delphi 7:在控制台应用程序中处理事件(TidIRC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 23:06