0

我想callbackmethod在它自己的线程中调用一个。将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(执行方法)中放置一个异常处理程序以捕获所有异常。像这样的东西:

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_PA 是否为空?Form_PAis.output 是否为空?之前也要检查语句的变量,因为归因于异常的行号有时会超出实际原因的一行。

于 2013-03-19T15:49:04.303 回答