G'Day,

See the code snippets below. My problem is as follows:

- TCricketLiveScoringClient creates an instance of TTXProcessThread
- In the Execute method it fires off the NewDataEvent() in the TCricketLiveScoringClient
- The NewDataEvent() then calls TCricketLiveScoringClient.ProcessTX() which uses a MIDAS socket connection to execute a mthod call of ProcessTX

The above however results in an "Error Reading From Socket" as soon as TCricketLiveScoringClient.ProcessTX() executes the MIDAS call. If I call TCricketLiveScoringClient.ProcessTX() in the context of the main application thread then all is well. So it appears to me that there is a problem when we cross thread boundaries. Can someone shed any light on this for me?

TIA



   TTXProcessThread = class(TThread)
   private
      FDatabase : TDataBase;
      FLastTxIDProcessed : integer;
      FTXTable : TTable;
      FNewTXDataEvent : TNewTXDataEvent;
   protected
      procedure Execute; override;
   public
      constructor Create(CreateSuspended : Boolean; ADBPath : string;
         AStartTXID : integer; ANewTXData : TNewTXDataEvent);
      destructor Destroy; override;
   end;

procedure TTXProcessThread.Execute;
////////////////////////////////////////////////////////////////////////////////
// PURPOSE: Main workhorse of the thread. It essentially looks for the next id
// in the table. Should it find it then it fires off an event with the info
// and then waits to ensure that things happened OK before proceeding round again
var
   EventCompleted : boolean;
   EventCompletedSuccessfully : boolean;
begin
   //   inherited;
   while not Terminated do
   begin
      with FTXTable do
      begin
      try
         if Locate('ID', FLastTxIDProcessed + 1, []) then
         begin
            //We have found the next TX
            //Fire off an event with this info
            FNewTXDataEvent(FLastTxIDProcessed + 1, FieldByName('SQLData').AsString, EventCompleted, EventCompletedSuccessfully);
            while not EventCompleted do
               Application.ProcessMessages;
            //The event completed.
            //Check to see whether it was successful
            if EventCompletedSuccessfully then
               Inc(FLastTxIDProcessed);
         end;
      except
          on E : Exception do
          ShowMessage(E.Message);
      end;
      end;
   end;
end;


type
   TCricketLiveScoringClient = class(TCustomPanel)
   private
      procedure NewDataEvent(ATXID : integer; ATXPayload : string;
         var EventCompleted : boolean; var EventCompletedSuccessfully : boolean);
   protected

   public
      function ProcessTX(AGlobalMatchCode : string; ATXID : Integer;
         const ATXPayload : string; var ANextTXID : Integer) : WordBool;
   published
      property Active : boolean read FActive write SetActive;
      property DBPath : string read FDBPath write FDBPath;
      property MidasServerAddress : string read FMidasServerAddress write SetMidasServerAddress;
      property MidasServerPort : SmallInt read FMidasServerPort write SetMidasServerPort;
   end;

function TCricketLiveScoringClient.ProcessTX(AGlobalMatchCode : string; ATXID : Integer;
   const ATXPayload : string; var ANextTXID : Integer) : WordBool;
////////////////////////////////////////////////////////////////////////////////
// PURPOSE: Processes the supplied TX information
var
   LocalNextTXID : integer;
begin
   //Show failure at this point
   result := FALSE;
   try
//TESTING
//      if ATXID <> (FLastTXProcessed + 1) then
//         raise ECricketLiveScoringClient.Create('TX is not the next in sequence.');
      if FSocketConnection.AppServer.ProcessTX(FClientIP, AGlobalMatchCode, ATXID,
         ATXPayload, LocalNextTXID) = WordBool(TRUE) then
      begin
         result := TRUE;
         FLastTXProcessed := ANextTXID - 1;
      end;
   except
      on E : Exception do
         raise ECricketLiveScoringClient.Create(E.Message);
   end;
end;

procedure TCricketLiveScoringClient.NewDataEvent(ATXID : integer; ATXPayload : string;
   var EventCompleted, EventCompletedSuccessfully : boolean);
////////////////////////////////////////////////////////////////////////////////
// PURPOSE: Fired when new data is detected in the TX table
var
   NextTXID : integer;
begin
   //Show that things are just started
   EventCompleted := FALSE;
   EventCompletedSuccessfully := FALSE;
   try
      try
         //Try and process the TX Line
         if not ProcessTX(FGlobalMatchCode, ATXID, ATXPayload, NextTXID) then
            raise ECricketLiveScoringClient.Create('I could not processs TXID ' + IntToStr(ATXID));
      finally
         //Show that event has completed
         EventCompleted := TRUE;
      end;
      //We got this far so things were successful
      EventCompletedSuccessfully := TRUE;
   except
      on E : Exception do
         raise ECricketLiveScoringClient.Create(E.Message);
   end;
end;


-- Donovan
----------------------------------------------------------------------
Donovan J. Edye [
www.edye.wattle.id.au]
Namadgi Systems [
www.namsys.com.au]
Voice: +61 2 6285-3460
Fax: +61 2 6285-3459
TVisualBasic = Class(None);
Heard just before the 'Big Bang': "...Uh Oh...."
----------------------------------------------------------------------
GXExplorer [
http://www.gxexplorer.org] Freeware Windows Explorer
replacement. Also includes freeware delphi windows explorer components.
----------------------------------------------------------------------

Reply via email to