0

callbackmethod独自のスレッドでa を呼び出したい。はcallbackMethodインターフェイスとして実装されます。

次のようなスレッドを宣言しました。

CustomfunctionCallbackThread = Class(TThread)
        protected
            procedure Execute; override;

        private
            var mCallId: String;
            var mCallbackMethod: ICustomfunctionCallback;
            var mParam: pCustomParam;
            var mError: integer;

            procedure callCallbackMethod;

        public
            procedure setData(callbackObject: pCustomfunctionCallbackObject);

     end; 

今、私はそのスレッドを次のように呼んでいます:

procedure classname.method(param :pCustomParam; callId: String; error: integer; callback: ICustomfunctionCallback);
var callbackObject: ^CustomfunctionCallbackObject;
var callbackThread: CustomfunctionCallbackThread;  
begin
  callbackObject.param:= param;
  callbackObject.error:= error;  
  callbackObject.callId:= callId; 
  callbackObject.callbackMethod:= callback; 

  callbackThread:= CustomfunctionCallbackThread.Create(true);
  callbackThread.setData(callbackObject);
  callbackThread.FreeOnTerminate:= true;
  callbackThread.Start;
end;

setData 関数は次のようになります。

procedure CustomfunctionCallbackThread.setData(callbackObject: pCustomfunctionCallbackObject);
begin
    mCallId:=callbackObject.callId;
    mParam:=callbackObject.param;
    mError:=callbackObject.error;
    mCallbackMethod:=callbackObject.callbackMethod;
end;

私の実行関数は次のようになります。

procedure CustomfunctionCallbackThread.Execute;
begin
    mCallbackMethod.callCustomfunctionCallback(mParam, mCallId, mError);
end; 

これで、コールバック メソッド (インターフェイス) は次のようになります。

procedure CustomfunctionCallback.callCustomfunctionCallback(param: pCustomParam; callId: String; error: integer);
var receivedCustomfunctionCallback: string;
begin
  receivedCustomfunctionCallback:= 'CustomfunctionCallback received: Param - ' +
    PAnsiChar(param^.getKey(0)) + ' | Value - ' + PAnsiChar(param^.getValue(0));

  Form_PAis.Utf8Convert(receivedCustomfunctionCallback);
  Dispose(param);
end;   

関数は正常に実行されますが、その後、デバッグ モードは自動的に終了します。

それがそのように見える場合:

procedure CustomfunctionCallback.callCustomfunctionCallback(param: pCustomParam; callId: String; error: integer);
var receivedCustomfunctionCallback: string;
begin
  receivedCustomfunctionCallback:= 'CustomfunctionCallback received: Param - ' +
    PAnsiChar(param^.getKey(0)) + ' | Value - ' + PAnsiChar(param^.getValue(0));

  Form_PAis.output.Append(receivedCustomfunctionCallback);
  Dispose(param);
end;

でクラッシュします(終了しません)

Form_PAis.output.Append(receivedCustomfunctionCallback);  

問題を解決する方法はありますか?

4

1 に答える 1

1

スレッドプロシージャをエスケープする例外により、プロセス全体がシャットダウンされます。すべての例外をトラップするには、threadproc(Executeメソッド)内に例外ハンドラーを配置します。このようなもの:

procedure CustomfunctionCallbackThread.Execute;
begin
  try
    mCallbackMethod.callCustomfunctionCallback(mParam, mCallId, mError);
  except
    on e: Exception do
    begin
      // output or log this error somewhere.
    end; 
  end;
end; 

上記のコードは、少なくともプロセス全体が失敗するのを防ぐのに役立つはずです。例外の原因を最初に見つけて修正する必要があります。

疑わしいForm_PAis.output...行またはその前に、コールバック関数にブレークポイントを設定します。ブレークポイントで停止するまでアプリを実行します。デバッガーで、ウォッチウィンドウまたはインスペクターを使用して、関連する変数とプロパティの値を確認します。Form_PAisはnullですか?Form_PAis.outputはnullですか?例外に起因する行番号が実際の原因を1行超えている場合があるため、前のステートメントの変数も確認してください。

于 2013-03-19T15:49:04.303 に答える