如何在Delphi中将异常从一个线程传递到另一个(调用者)线程?

编程入门 行业动态 更新时间:2024-10-22 23:42:54
本文介绍了如何在Delphi中将异常从一个线程传递到另一个(调用者)线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我还有一个问题!请看下面的例子:

I have another question! Please look at this example:

// There is a class with some method: type TMyClass = class public procedure Proc1; end; // There is a some thread class: TMyThread = class(TThread) protected procedure Execute; override; end; procedure TMyClass.Proc1; begin // this method is just calling another thread: with TMyThread.Create(True) do begin FreeOnTerminate := True; Resume; end; // + there are some more actions end; procedure TMyThread.Execute; begin // in this example this thread just throws exception: raise Exception.Create('Some exception'); end;

所有我想要的-是在TMyClass.Proc1中引发异常并像这样抛出它:

All what i want - is to get raised exception in the TMyClass.Proc1 and throw it up like this:

var myObject: TMyClass; begin myObject := TMyClass.Create; try myObject.Proc1; // launch and watch what happenings except on E: Exception do WriteErrorLog(E.Message); end; FreeAndNil(myObject); end;

请告诉我如何制作这样的东西? 非常感谢!

Please tell me how can i make something like this? Many Thanks!

哦!还有一件事-我在Delphi 5上进行即时编码,因此我没有TThread的"FatalException"属性或其他相关内容.

oh! one more thing - im coding on Delphi 5 so i have'nt "FatalException" property of TThread or something about..

推荐答案

您可以使用 AcquireExceptionObject() :

You can use AcquireExceptionObject():

AcquireExceptionObject返回一个指向当前异常对象的指针,并防止在当前异常处理程序退出时释放该异常对象.

AcquireExceptionObject returns a pointer to the current exception object and prevents the exception object from being deallocated when the current exception handler exits.

然后,您可以将指针发送到另一个线程,如果将其引发,它将为您释放,否则,您必须调用 ReleaseExceptionObject() 来释放它.

Then you can send the pointer to another thread and if you raise it there it will be freed for you, otherwise you must call ReleaseExceptionObject() to free it.

更多推荐

如何在Delphi中将异常从一个线程传递到另一个(调用者)线程?

本文发布于:2023-11-11 23:33:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   中将   异常   如何在   调用者

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!