Link to home
Create AccountLog in
Avatar of Kinger247
Kinger247

asked on

How to crash a .net application ?

Ok, I'm that good a coder I cant get my application to crash ... :)
But for the sake of testing, I need to mimic a complete crash of my application (ie it just shuts down).
I could do a alt-f4, but I want to really have something in code to hit and crash.

The reason why I want to do this is because I have the app connected to a vb.net server via tcp and when the client app crashes, the server doesnt recognise its gone.
But only when it crashes.

Any ideas ?
cheers K.
Avatar of abel
abel
Flag of Netherlands image

What about creating a button with an illegal operation under it, i.e., division by zero? You have to disguise it a bit, otherwise it won't compile...
This works for me:


private void Q24183289_Click(object sender, EventArgs e)
{
    int i = 0;
    int j = 0;
    int result = i / j;   // raises DivideByZeroException unhandled
}

Open in new window

Maybe that's not "crashy" enough? I just found out that the .NET runtime presents me with a very nice error message, allowing me to continue...

ScreenShot081.png
Avatar of Kinger247
Kinger247

ASKER

yes, I need it to crash, no error just a complete vanish ...
So throwing an exception is no good eg:

throw new Exception("Blah");
When an exception is raised in your app then it will be handled by and try catch blocks in your code and if it remains unhandled then it will be handled by the .net runtime hence the error message you posted an image of.
It sounds like you are wanting the app to crash and the .net runtime to crash too!
 
try to call a procedure recursively without any exit pooint, it should throw stack overflow error. not sure if it displays same continue/quit window or it crashes. right now i dont have a system with VS installed.

try something like this

private sub callMeAgain()
           callMeAgain()
           callMeAgain()
end sub


and from button click event call callMeAgain() sub
Better try it on your pc and not mine!
Only need callMeAgain once though, the second one wil never be reached :)
I dont know if this will help you but you could download "Debugging tools for Windows" and run a debugger on the application, then hit CTRL-C to abort the debugger (this will crash the application), but Im not sure what you want to achieve with the crash since I thing you will get an error related to the Attached debugger or Application shutdown or something like that.
But if you want to try just download and install Debugging tools for windows that you will find here

 
Then you start your application and open a command shell, goto the folder for "Debugging tools for windows" and run this
 
cscript adplus.vbs -crash -pn [application name]

Then you will be presented with a debugging window (minimized), select that window and press CTRL-C (maybe twice), this will dump the memory for your application and quit the application. but as I said before this might not be what you wanted as it might present you with a misleading crash cause. But at least this is working for a windows sevice where service control manager log this as "terminated unexpectedly"
/ Mikael
Sorry forgot the link to the debugging tools for windows
http://www.microsoft.com/whdc/devtools/debugging/installx86.mspx#a
/ Mikael
This will do what you want, the trick is to override the default exception handler and then to raise an exception. I deducted this from info on http://www.codeproject.com/KB/dotnet/unhandledexceptions.aspx and http://msdn.microsoft.com/en-us/magazine/cc188720.aspx, the latter being a quite good read.

The screenshot I applied can be different depending on your windows version.

private void Q24183289_Click(object sender, EventArgs e)
{
    //AppDomain.CurrentDomain.UnhandledException += // CLR
    //    new UnhandledExceptionEventHandler(OnUnhandledException);
    Application.ThreadException += // Windows Forms
         new System.Threading.ThreadExceptionEventHandler(
             OnGuiUnhandedException);
 
 
    int i = 0;
    int j = 0;
    int result = i / j;  // throws exception
}
 
// Windows Forms unhandled exception
public static void OnGuiUnhandedException(Object sender,
   System.Threading.ThreadExceptionEventArgs e)
{
    throw new InvalidOperationException(
        "Invalid operation.");
 
}

Open in new window

ScreenShot082.png
> then it will be handled by the .net runtime hence the error message you posted an image of.

I was the one showing that image ;)
Using the code above, the .NET runtime will not be the one that takes over the error handling, because you just overrid it with the line

   Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);

You can use the other (commented) line if it is a console application.
>>Only need callMeAgain once though, the second one wil never be reached :)
yeah, while doing copy paste did it twice:)
do you know hwo to this part in vb.net  ?

Application.ThreadException += // Windows Forms
         new System.Threading.ThreadExceptionEventHandler(OnGuiUnhandedException);

cant get the Application.ThreadException part working.
Seems to be what I need though :)
> do you know hwo to this part in vb.net  ?

I can give it a try. Hold on.
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
cheers for your help.
You're welcome :)