Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 21-Apr-12 8:20am
pucx1.2K
Edited 22-Apr-12 6:29am
v2
Comments
Philippe Mori at 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 at 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.
SAKryukov at 22-Apr-12 20:47pm
   
OK, this is a deep question, so I answered and voted 5 for the question.
--SA
SAKryukov at 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 at 23-Apr-12 2:14am
   
yeah
SAKryukov at 25-Apr-12 17:05pm
   
Yeah? It was the alternative question. :-)
Anyway, my answer is comprehensive enough, please see.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
pucx at 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 at 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.
SAKryukov at 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 at 22-Apr-12 11:40am
   
Good answer. 5!
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
VJ Reddy at 22-Apr-12 21:24pm
   
Nice answer with informative references. 5!
SAKryukov at 22-Apr-12 21:39pm
   
Thank you, VJ.
--SA
Abhinav S at 22-Apr-12 23:55pm
   
Detailed. My 5.
SAKryukov at 23-Apr-12 0:45am
   
Thank you, Abhinav.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  
Comments
phil.o at 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 at 22-Apr-12 12:32pm
   
Thank you, phil.
SAKryukov at 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 at 22-Apr-12 21:26pm
   
Thank you, SA.
Seen your answer. It really gives a comprehensive picture of the problem stated in the question.
SAKryukov at 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
Abhinav S at 22-Apr-12 23:55pm
   
Correct 6.
VJ Reddy at 23-Apr-12 0:00am
   
Thank you, Abhinav.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
pucx at 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.
SAKryukov at 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 at 22-Apr-12 11:40am
   
Good answer. 5!
Abhinav S at 22-Apr-12 23:54pm
   
Thank you VJ.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi,
 
Just write the logs before and after calling the function (Messagebox is other option).
Another option is to debug your program..
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 400
1 Jochen Arndt 200
2 Richard MacCutchan 135
3 DamithSL 95
4 Maciej Los 95
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,032
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,220


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 23 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100