Ada 83(仅)如何结束包含多个步骤的整个程序 [英] Ada 83(Only) How to end an entire program which has multiple procedures

查看:93
本文介绍了Ada 83(仅)如何结束包含多个步骤的整个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ada83(使用此版本是一个课程要求),我正在使用多个步骤。我不知道如何学习整个程序。诸如C程序中的Exit之类的东西会关闭整个过程。

I am using Ada83 (It's a course requirement to use this version), I am using multiple procedures. I don't know how to come out of the entire program. Some thing like Exit in C program which closes entire procedure. Where is the exit is called from?

推荐答案

如果程序不使用任务,则可以定义一个表示紧急出口;也许在某些软件包中:

If your program does not use tasks, you can define an exception that signifies an "emergency exit"; perhaps in some package:

package Emergency is
    Emergency_Exit : exception;
end Emergency;

在您的主过程中,请捕获以下异常:

In your main procedure, catch this exception:

procedure Your_Main_Procedure is
begin 
    ... whatever the main procedure does
exception
    when Emergency.Emergency_Exit =>
        null;
end Your_Main_Procedure;

因此,无论何时在程序中的某个地方引发异常:

So that whenever you raise the exception somewhere in your program:

raise Emergency_Exit;

它将控制权转移到 null 语句,然后它将到达主过程的结尾并退出程序。

it will transfer control to the null statement, which will then reach the end of the main procedure and exit the program.

以这种方式执行操作意味着您可以向其他过程添加清除代码:

Doing it this way means you can add cleanup code to other procedures:

procedure Something_Else is
    ...
begin
    Direct_IO_Instance.Open (Some_File, Direct_IO_Instance.Out_File, "filename");
    begin
        ... do your work
    exception
        when Emergency.Emergency_Exit =>
                -- cleanup 
            Finish_Writing (Some_File);
            Direct_IO_Instance.Close (Some_File);
                -- now reraise, which will eventually reach the end of the program
            raise;
    end;
end Something_Else;

因此,当提高 Emergency_Exit 时,它将最终将控制权转移到主过程的末尾,但是在执行任何所需的清除的过程中,它可能会在其他异常处理程序中停止运行。

So when Emergency_Exit is raised, it will eventually transfer control to the end of the main procedure, but it might stop off in other exception handlers along the way to do any needed cleanup.

如果其他任务正在运行,我认为这不起作用,因为主程序会在程序退出之前等待其他任务完成。在这种情况下,在Ada 83中,您需要协调出口与其他任务;也许您可以定义一个全局布尔值,使这些任务定期检查以使它们退出,或者您可以以某种方式构造程序,以使 Emergency_Exit 异常处理程序知道哪些任务终止,或者可以调用任务条目以使这些任务终止。最佳解决方案将取决于实际的程序结构。

I don't think this works if there are other tasks running, because the main procedure will wait for those other tasks to complete before the program exits. In that case, in Ada 83, you'll need to coordinate the exit with the other tasks; perhaps you can define a global Boolean that those tasks check periodically to get them to exit, or perhaps you can structure the program in a way so that the Emergency_Exit exception handler knows what tasks to abort, or can call a task entry to get those tasks to terminate. The best solution would depend on the actual program structure.

这篇关于Ada 83(仅)如何结束包含多个步骤的整个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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