C#如何判断程序代码是否卡住. [英] C # how to judge whether a program code stuck.

查看:227
本文介绍了C#如何判断程序代码是否卡住.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#如何确定程序或函数的代码是否卡住.谢谢.
我只想检测一段代码或该函数的功能,例如我的自定义函数fun(),如果函数卡住或无限循环,我想检测此fun函数的操作,那么IIf不会卡住是另一种后续治疗方法,我想做到这一点,专家建议采取措施进行后续处理,谢谢.
我不是如何调试编译器以发现问题,我是为了防止某个函数在程序中运行,可能存在无限循环或卡死导致的意外问题,谢谢.

C # How to determine the code of a program or a function is stuck. Thank you.
I just want to detect a piece of code or a function of the operation, such as my custom function fun(), I would like to detect the operation of this fun function if the function is stuck or is infinite loop, then IIf you do not get stuck is another case of follow-up treatment, I wanted to achieve this, experts advise to take a measure for subsequent processing, Thank you.
I''m not how to debug the compiler to identify problems, I am in order to prevent a function in the program is running, there may be some unexpected problems caused by infinite loop or stuck, thank you.

推荐答案

在要检查的方法的开头放置一个断点.

然后按F5键并开始调试.当程序遇到断点时,它将暂停并且Visual Studio将进入调试模式.从那里可以逐行执行代码(按F10或F11),并注意变量的内容.

那里 [
Put a breakpoint at the beginning of the method you want to check.

Then press F5 and start debugging. When the program will meet your breakpoint, it will pause and Visual Studio will come up in debug mode. From there you can execute your code line by line (press F10, or F11) and watch for the content of your variables.

There[^] you will find all that you need to catch the basics of debugging.


问题一点都不简单.看起来没有任何替代调试的实用方法,但是我们对更好的东西抱有遥远而有限的希望.

假设我们需要对无限循环问题进行一些确定的自动化诊断.它可以嵌入到有问题的程序中,也可以基于静态代码分析,无论是编译的代码还是源代码.

严格来说,这是完全不可能的.这在数学上得到了证明,并且在可计算性理论中被称为"停止问题".通过任何类型的代码分析,理论上不可能预测某个程序是否会终止.请参阅:
http://en.wikipedia.org/wiki/Computability_theory_%28computer_science%29 [ ^ ],
http://en.wikipedia.org/wiki/Halting_problem [无法解决.尽管如此,还是有希望的.如果看一下定理的证明,就会发现这个想法是这样的:假设存在解决停顿问题的算法;我们将其命名为A.我们可以使用A设计这样的程序P,该程序以A失败的方式工作.因此,一个充满希望的问题是:我们能否制定一些可以预测行为的非常广泛的,具有实际意义的程序子类. (当然,您可以找到许多琐碎的程序子类,例如,没有派生,没有循环的程序等.这种发现实际上没有任何重要意义.)

几年前,我只看到一篇文章告诉我们,微软已经朝这个方向进行了一些研究.到目前为止,我从未听说过任何结果.因此,目前调试和仅使用人脑是唯一的选择.

—SA
The problem is not simple at all. It looks like there is no any practical alternative to debugging, but we have some remote and limited hope for something better.

Let''s say we need some definitive automated diagnostics of the infinite loop problem. It can be embedded in the program in question or based on static code analysis, no matter compiled or source code.

Strictly speaking, this is totally impossible. This is proven mathematically and is known in computability theory as the "halting problem". With any kind of code analysis, it is theoretically impossible to predict if some program will ever terminate or not. Please see:
http://en.wikipedia.org/wiki/Computability_theory_%28computer_science%29[^],
http://en.wikipedia.org/wiki/Halting_problem[^].

So, the most general problem formulated as "give me the way to predict if some program can get in an infinite loop or not" is the ill-posed problem and cannot be solved. Nevertheless, there is some hope. If you look at the proof of the theorem, you will see that the idea is this: suppose the algorithm for solving of the halting problem exist; let''s name it A. We can design such a program P using A that works the way A fails. So the hopeful question is: can we formulate some very wide practically significant subclass of programs which behavior could be predicted. (Of course you can find many trivial subclasses of programs, for example, programs with no forks, or not loops, etc. Such finding would not have any practically important sense.)

Several years ago I saw just one article telling us that Microsoft started some research in this direction. I never heard about any results so far. So, right now debugging and just using human brain is the only option.

—SA


给出的解决方案1和2都是很好的答案.

.NET中处理错误或异常的建议做法是将代码包装在try块中,如果发生异常,请在相应的catch块中处理该异常.然后,要在非失败或try块中的代码失败的情况下执行任何代码,可以使用finally块.此处说明了异常处理.
http://msdn.microsoft.com/en-us/library/s7fekhdy (v = vs.71).aspx [ http://www.dotnetperls.com/debugging [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]
Both Solution 1 and 2 given are good answers.

The recommended practice in .NET to handle errors or exceptions is to wrap the code within the try block and in case of exception, deal with that exception in the corresponding catch block. Then, to execute any code either in case of non failure or failure of the code within the try block, the finally block can be used. Exception handling is explained here.
http://msdn.microsoft.com/en-us/library/s7fekhdy(v=vs.71).aspx[^]
But, in case of infinite loop, the program will run until any error occurs, which may be very long time, till that time no response may be there even with the try catch block. So, I think it is wise to debug the program and observe whether any infinite loop is being executed. If you are new to debugging it is better to learn debugging and it will be a very good help in writing the program and a real time saver.
In this regard I think the following articles may be helpful
http://www.dotnetperls.com/debugging[^]
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]


这篇关于C#如何判断程序代码是否卡住.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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