如何编写Delphi的Abort过程的c#版本 [英] How to write c# version of Delphi's Abort procedure

查看:74
本文介绍了如何编写Delphi的Abort过程的c#版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi有一个名为Abort的程序。以下内容摘自Delphi帮助:

使用Abort从执行路径中逃脱而不报告错误.Abort引发一个特殊的静默异常 (EAbort),其运行方式与任何其他异常类似,但不向最终用户显示错误消息。中止将执行重定向到最后一次尝试的结尾.. finally块。

 EAbort = class(Exception); 
程序中止;
function ReturnAddr:指针;
asm
MOV EAX,[EBP - 4 ]
end ;
开始
在ReturnAddr处提升EAbort.Create(SOperationAborted);
end ;



我现在要去.Net了。我找不到任何方法类似于中止程序。是否可以编写这样的Delphi'的Abort程序的c#版本?

 尝试  //  应用程序级别尝试.. finally块 
{
尝试
{
...
尝试 // 当前级别尝试..最后阻止
{
...
// 抛出一个只会被应用程序级别捕获的SilentException尝试.. finally阻止
// 并将执行重定向到应用程序级别的结尾尝试.. finally block
Abort; // 如何????????????????????? ????
...
}
catch
{
}
}
catch
{
}
}
catch ()
{
}
最后
{
}
...



任何人都可以帮助我吗?任何建议都是合适的。

非常感谢。 。

解决方案

它已经实现:抛出异常并且catch位于某个高级堆栈帧的某个位置。这就是你所需要的。



现在,你可以定义你的自定义异常,但是已经存在一个合适的声明: System.Threading.ThreadAbortException http://msdn.microsoft.com/en-us /library/system.threading.threadabortexception.aspx [ ^ ]。



不仅可以将它用于异步线程中止(也就是说,来自其他一些线程,通过使用 Thread.Abort ),但你可以完全按照你的目的使用它。



问题解决了。







当然,没有什么比你的内联汇编更让你(我也是)很乐意使用用Delphi适用。 CLR非常不同:代码被编译成CIL(通用中间语言),而不是CPU指令。当使用JIT编译CIL时,CPU开始起作用。请参阅:

http://en.wikipedia.org/wiki/Common_Intermediate_Language [< a href =http://en.wikipedia.org/wiki/Common_Intermediate_Languagetarget =_ blanktitle =New Window> ^ ],

http://en.wikipedia.org/wiki/Just-in-time_compilation [ ^ ]。



< DD> -SA

Delphi has a precedure named "Abort".The following is picked up from Delphi help:
Use Abort to escape from an execution path without reporting an error.Abort raises a special "silent exception" (EAbort), which operates like any other exception, but does not display an error message to the end user. Abort redirects execution to the end of the last try .. finally block.

EAbort = class(Exception);
procedure Abort;
  function ReturnAddr: Pointer;
  asm
          MOV     EAX,[EBP - 4]
  end;
begin
  raise EAbort.Create(SOperationAborted) at ReturnAddr;
end;


I''m now going to .Net. I can not find any method Similar to the "Abort" procedure.Is it possible to to write c# version of Delphi''s "Abort" procedure like this?

try //application level try .. finally block
{
  try
  {
    ...
    try //Current level try .. finally block
    {
      ...
      // Throw a SilentException that will only catched by application level try .. finally block
      // and redirects execution to the end of application level try .. finally block
      Abort; //How to??????????????????????????
      ... 
    }
    catch 
    {
    }
  }
  catch 
  {
  }
}
catch ()
{
}
finally
{
}
...


Can anyone help me? Any suggestion wil be appropriate.
Thanks a lot. .

解决方案

It is already implemented: throw an exception and catch is in some location on some upper-level stack frame. This is all you need.

Now, you can define your custom exception, but one suitable declaration already exists: System.Threading.ThreadAbortException: http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx[^].

Not only you can use it for asynchronous thread abort (that is, from some other thread, by using Thread.Abort), but you can use it exactly for your purpose.

Problem solved.

[EDIT]

And of course, nothing like your inline assembly you (and me, too) gladly used with Delphi is applicable. CLR is very different: code is compiled into CIL (Common Intermediate Language), not CPU instructions. CPU comes into play when CIL is compiled with JIT. Please see:
http://en.wikipedia.org/wiki/Common_Intermediate_Language[^],
http://en.wikipedia.org/wiki/Just-in-time_compilation[^].

—SA


这篇关于如何编写Delphi的Abort过程的c#版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆