Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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.
Posted
Updated 22-Apr-12 5:29am
v2
Comments
Philippe Mori 21-Apr-12 14:41pm    
While debugging, execute "Break" command from the menu. The debugger will stop at the current location. Repeat that a few time and check where it break. You can also add some breakpoints to confirm if some code get executed.
pucx 22-Apr-12 11:29am    
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.
Sergey Alexandrovich Kryukov 22-Apr-12 20:47pm    
OK, this is a deep question, so I answered and voted 5 for the question.
--SA
Sergey Alexandrovich Kryukov 22-Apr-12 20:28pm    
Where do you want to detect it? During run-time? Or do you want to have some utility which would analyze your code and (compiled or not) and give you the answer?
--SA
pucx 23-Apr-12 2:14am    
yeah

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.
 
Share this answer
 
Comments
pucx 22-Apr-12 11:28am    
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.
phil.o 22-Apr-12 12:26pm    
So I do not understand your question.

Take the time to formulate it in a way we can understand it.
Sergey Alexandrovich Kryukov 22-Apr-12 20:47pm    
Well, I do understand it. The question is correct, even though it is proven that such algorithm cannot be created.
Please see my answer.
--SA
VJ Reddy 22-Apr-12 11:40am    
Good answer. 5!
Sergey Alexandrovich Kryukov 22-Apr-12 20:45pm    
This makes perfect sense but does not answer the question; the problem is much deeper. You would need to explain why is it so. You need to know about well-known halting problem of the computability theory.
I'll vote 4, but -- please see my answer.
--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
 
Share this answer
 
Comments
VJ Reddy 22-Apr-12 21:24pm    
Nice answer with informative references. 5!
Sergey Alexandrovich Kryukov 22-Apr-12 21:39pm    
Thank you, VJ.
--SA
Abhinav S 22-Apr-12 23:55pm    
Detailed. My 5.
Sergey Alexandrovich Kryukov 23-Apr-12 0:45am    
Thank you, Abhinav.
--SA
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[^]
 
Share this answer
 
Comments
phil.o 22-Apr-12 12:27pm    
Nice one also. 5ed!
But I'm not really sure that it's what the OP is after.
VJ Reddy 22-Apr-12 12:32pm    
Thank you, phil.
Sergey Alexandrovich Kryukov 22-Apr-12 20:44pm    
VJ, this is practically sensible answer, but the problem is much deeper. You would need to explain why is it so.
You need to know about well-known halting problem of the computability theory.
I'll vote 4, but -- please see my answer.
--SA
VJ Reddy 22-Apr-12 21:26pm    
Thank you, SA.
Seen your answer. It really gives a comprehensive picture of the problem stated in the question.
Sergey Alexandrovich Kryukov 22-Apr-12 22:03pm    
Thank you, VJ.
The halting problem is something good to know for every developer. However, I like OP's thinking, so I also voted 5 for the question.
--SA
Why don't you just step through / debug your program? That should give you a fair idea of what is going on inside your code.

There are tools like WinDbg which could help you trace a deployed application.
 
Share this answer
 
Comments
pucx 22-Apr-12 11:28am    
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.
Sergey Alexandrovich Kryukov 22-Apr-12 22:02pm    
You are right, but as I explain in my solution, the problem as you formulate it is not solvable theoretically. Please see.

@Abhinav: therefore I voted 4. Please see my other comments and the solution.
--SA
VJ Reddy 22-Apr-12 11:40am    
Good answer. 5!
Abhinav S 22-Apr-12 23:54pm    
Thank you VJ.
Hi,

Just write the logs before and after calling the function (Messagebox is other option).
Another option is to debug your program..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900